MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL I/O Operations

I/O (Input/Output) operations in COBOL involve reading data from and writing data to various sources. This includes file I/O (reading from and writing to files), console I/O (interactive input and output), and system I/O (getting date, time, and system information). Understanding I/O operations is essential for building COBOL programs that process data, interact with users, manage files, and integrate with system services in mainframe environments.

What are I/O Operations?

I/O operations encompass all data input and output:

  • File I/O: Reading from and writing to files (READ, WRITE, REWRITE, DELETE)
  • Console I/O: Interactive input and output (ACCEPT, DISPLAY)
  • System I/O: Getting system information (date, time, environment)
  • Printer I/O: Writing to printers and reports
  • Network I/O: Communicating over networks (web services, sockets)

I/O operations enable COBOL programs to receive data, process it, and produce output.

Console Input: ACCEPT Statement

ACCEPT reads data from the standard input device (console/terminal):

Basic ACCEPT Syntax

cobol
1
2
3
4
ACCEPT identifier *> Or with source specification ACCEPT identifier FROM source

Example: Reading User Input

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
WORKING-STORAGE SECTION. 01 USER-NAME PIC X(30). 01 USER-AGE PIC 9(3). 01 USER-AMOUNT PIC 9(8)V99. PROCEDURE DIVISION. MAIN-PARA. DISPLAY "Enter your name: " WITH NO ADVANCING ACCEPT USER-NAME DISPLAY "Enter your age: " WITH NO ADVANCING ACCEPT USER-AGE DISPLAY "Enter amount: " WITH NO ADVANCING ACCEPT USER-AMOUNT DISPLAY "Name: " USER-NAME DISPLAY "Age: " USER-AGE DISPLAY "Amount: " USER-AMOUNT STOP RUN.

ACCEPT FROM DATE and TIME

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
WORKING-STORAGE SECTION. 01 CURRENT-DATE PIC 9(8). 01 CURRENT-TIME PIC 9(6). 01 CURRENT-DAY PIC 9(7). PROCEDURE DIVISION. MAIN-PARA. *> Get current date (YYYYMMDD format) ACCEPT CURRENT-DATE FROM DATE YYYYMMDD *> Get current time (HHMMSS format) ACCEPT CURRENT-TIME FROM TIME *> Get current day (YYYYDDD format - year and day of year) ACCEPT CURRENT-DAY FROM DAY YYYYDDD DISPLAY "Date: " CURRENT-DATE DISPLAY "Time: " CURRENT-TIME DISPLAY "Day: " CURRENT-DAY STOP RUN.

ACCEPT FROM retrieves system information like date and time, which is useful for timestamps, logging, and date-based processing.

Console Output: DISPLAY Statement

DISPLAY writes data to the standard output device (console/terminal):

Basic DISPLAY Syntax

cobol
1
2
3
DISPLAY identifier-1 [identifier-2 ...] [UPON mnemonic-name] [WITH NO ADVANCING]

Example: Displaying Output

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
WORKING-STORAGE SECTION. 01 CUSTOMER-NAME PIC X(30) VALUE 'JOHN SMITH'. 01 ACCOUNT-BALANCE PIC 9(8)V99 VALUE 1000.50. PROCEDURE DIVISION. MAIN-PARA. *> Display literal DISPLAY "Welcome to Customer System" *> Display variable DISPLAY CUSTOMER-NAME *> Display combination DISPLAY "Customer: " CUSTOMER-NAME DISPLAY "Balance: $" ACCOUNT-BALANCE *> Display with no advancing DISPLAY "Processing" WITH NO ADVANCING DISPLAY "..." WITH NO ADVANCING DISPLAY " Complete" *> Output: "Processing... Complete" on one line STOP RUN.

File Input: READ Statement

READ retrieves records from files:

Sequential File Reading

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
PROCEDURE DIVISION. MAIN-PARA. OPEN INPUT CUSTOMER-FILE PERFORM UNTIL END-OF-FILE READ CUSTOMER-FILE AT END SET END-OF-FILE TO TRUE DISPLAY "End of file reached" NOT AT END DISPLAY "Customer: " CUSTOMER-NAME *> Process record END-READ END-PERFORM CLOSE CUSTOMER-FILE STOP RUN.

Random Access Reading

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
PROCEDURE DIVISION. MAIN-PARA. OPEN INPUT CUSTOMER-FILE *> Set key for random access MOVE 12345 TO CUSTOMER-ID READ CUSTOMER-FILE INVALID KEY DISPLAY "Customer not found: " CUSTOMER-ID NOT INVALID KEY DISPLAY "Customer: " CUSTOMER-NAME DISPLAY "Balance: " CUSTOMER-BALANCE END-READ CLOSE CUSTOMER-FILE STOP RUN.

File Output: WRITE Statement

WRITE adds records to files:

Writing to Sequential Files

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
PROCEDURE DIVISION. MAIN-PARA. OPEN OUTPUT REPORT-FILE *> Prepare record data MOVE "Report Header" TO REPORT-LINE WRITE REPORT-LINE MOVE "Data Line 1" TO REPORT-LINE WRITE REPORT-LINE *> Write with line advance WRITE REPORT-LINE AFTER ADVANCING 2 LINES CLOSE REPORT-FILE STOP RUN.

Writing to Indexed Files

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
PROCEDURE DIVISION. MAIN-PARA. OPEN OUTPUT CUSTOMER-FILE *> Set key and data MOVE 12345 TO CUSTOMER-ID MOVE 'JOHN SMITH' TO CUSTOMER-NAME MOVE 1000.50 TO CUSTOMER-BALANCE *> Write record WRITE CUSTOMER-RECORD INVALID KEY DISPLAY "Error: Duplicate key" NOT INVALID KEY DISPLAY "Record written successfully" END-WRITE CLOSE CUSTOMER-FILE STOP RUN.

Updating Records: REWRITE Statement

REWRITE updates existing records:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
PROCEDURE DIVISION. MAIN-PARA. OPEN I-O CUSTOMER-FILE *> Read record to update MOVE 12345 TO CUSTOMER-ID READ CUSTOMER-FILE INVALID KEY DISPLAY "Record not found" NOT INVALID KEY *> Modify record ADD 100.00 TO CUSTOMER-BALANCE *> Write updated record back REWRITE CUSTOMER-RECORD INVALID KEY DISPLAY "Update failed" NOT INVALID KEY DISPLAY "Record updated" END-REWRITE END-READ CLOSE CUSTOMER-FILE STOP RUN.

REWRITE replaces the record that was last read. You must read a record before you can rewrite it.

Deleting Records: DELETE Statement

DELETE removes records from files:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
PROCEDURE DIVISION. MAIN-PARA. OPEN I-O CUSTOMER-FILE *> Set key of record to delete MOVE 12345 TO CUSTOMER-ID *> Delete the record DELETE CUSTOMER-FILE RECORD INVALID KEY DISPLAY "Record not found: " CUSTOMER-ID NOT INVALID KEY DISPLAY "Record deleted successfully" END-DELETE CLOSE CUSTOMER-FILE STOP RUN.

File Opening and Closing

Files must be opened before use and closed when done:

Opening Files

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
*> INPUT - Read only OPEN INPUT CUSTOMER-FILE *> OUTPUT - Write only (creates new, deletes existing) OPEN OUTPUT CUSTOMER-FILE *> I-O - Read and write (for updates) OPEN I-O CUSTOMER-FILE *> EXTEND - Append to existing file OPEN EXTEND LOG-FILE *> Open multiple files OPEN INPUT INPUT-FILE OUTPUT OUTPUT-FILE I-O UPDATE-FILE

Closing Files

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
*> Close single file CLOSE CUSTOMER-FILE *> Close multiple files CLOSE CUSTOMER-FILE EMPLOYEE-FILE TRANSACTION-FILE *> Always close files, even on errors IF ERROR-OCCURRED CLOSE CUSTOMER-FILE STOP RUN END-IF

File Status Codes

File status codes indicate operation results:

Common File Status Codes
Status CodeMeaningWhen It Occurs
'00'SuccessOperation completed successfully
'10'End of fileNo more records (sequential read)
'23'Record not foundKey not found (random access)
'22'Duplicate keyAttempt to write duplicate key
'30'Permanent I/O errorHardware or file system error
'24'Boundary violationWrite beyond file limits

I/O Operation Patterns

Pattern 1: Interactive Input/Output

cobol
1
2
3
4
5
6
7
8
*> Get input from user DISPLAY "Enter customer ID: " WITH NO ADVANCING ACCEPT CUSTOMER-ID *> Process and display result DISPLAY "Processing customer " CUSTOMER-ID "..." *> ... processing ... DISPLAY "Customer processed successfully"

Pattern 2: File Processing Loop

cobol
1
2
3
4
5
6
7
8
9
10
*> Process all records in file OPEN INPUT INPUT-FILE PERFORM UNTIL END-OF-FILE READ INPUT-FILE AT END SET END-OF-FILE TO TRUE NOT AT END PERFORM PROCESS-RECORD END-READ END-PERFORM CLOSE INPUT-FILE

Pattern 3: Lookup and Display

cobol
1
2
3
4
5
6
7
8
9
10
*> Look up record and display MOVE SEARCH-KEY TO RECORD-KEY READ FILE-NAME INVALID KEY DISPLAY "Record not found: " SEARCH-KEY NOT INVALID KEY DISPLAY "Record found:" DISPLAY " ID: " RECORD-ID DISPLAY " Name: " RECORD-NAME END-READ

Best Practices for I/O Operations

Follow these best practices:

  • Always check file status: Check FILE STATUS after every file operation
  • Handle end of file: Use AT END for sequential reads
  • Use INVALID KEY: Handle INVALID KEY for random access
  • Close files properly: Always close files, even on errors
  • Validate input: Validate user input from ACCEPT before processing
  • Provide user feedback: Use DISPLAY to inform users of progress and errors
  • Use appropriate open mode: Choose INPUT, OUTPUT, I-O, or EXTEND based on needs
  • Handle errors gracefully: Provide meaningful error messages
  • Read before update: Always read a record before updating it
  • Set keys correctly: Ensure keys are set before random access operations

Explain Like I'm 5: I/O Operations

Think of I/O operations like talking and listening:

  • ACCEPT is like listening - your program listens to what the user types
  • DISPLAY is like talking - your program speaks and shows information
  • READ is like reading a book - your program reads information from files
  • WRITE is like writing in a notebook - your program writes information to files
  • Opening a file is like opening a book - you need to open it before reading
  • Closing a file is like closing a book - you close it when you're done

So I/O operations are ways for your COBOL program to communicate - listening to users, talking to users, reading from files, and writing to files!

Practice Exercises

Complete these exercises to reinforce your understanding:

Exercise 1: Interactive Program

Create a program that uses ACCEPT to get user input (name, age, city) and DISPLAY to show the information back to the user in a formatted way.

Exercise 2: File Reading

Create a program that opens a sequential file, reads all records, and displays each record using DISPLAY. Handle end of file appropriately.

Exercise 3: Date and Time Display

Create a program that gets the current date and time using ACCEPT FROM, formats them nicely, and displays them using DISPLAY.

Exercise 4: File Writing

Create a program that accepts data from the user, writes it to a file using WRITE, and confirms the write was successful using DISPLAY.

Exercise 5: Complete I/O Program

Create a program that reads records from an input file, processes them, writes results to an output file, and displays progress messages to the console using DISPLAY.

Test Your Knowledge

1. What statement is used for console input in COBOL?

  • READ
  • ACCEPT
  • INPUT
  • GET

2. What statement is used for console output in COBOL?

  • WRITE
  • DISPLAY
  • OUTPUT
  • PRINT

3. What is the difference between ACCEPT and READ?

  • There is no difference
  • ACCEPT is for console input, READ is for file input
  • ACCEPT is for files, READ is for console
  • ACCEPT is faster than READ

4. How do you get the current date in COBOL?

  • ACCEPT FROM DATE
  • GET CURRENT DATE
  • READ DATE
  • DISPLAY DATE

5. What is required before using READ or WRITE?

  • ACCEPT statement
  • File must be opened
  • DISPLAY statement
  • Nothing is required

6. What does WITH NO ADVANCING do in DISPLAY?

  • Prevents output
  • Prevents cursor from advancing to next line
  • Displays in uppercase
  • Displays without formatting

Related Concepts

Related Pages