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.
I/O operations encompass all data input and output:
I/O operations enable COBOL programs to receive data, process it, and produce output.
ACCEPT reads data from the standard input device (console/terminal):
1234ACCEPT identifier *> Or with source specification ACCEPT identifier FROM source
123456789101112131415161718192021WORKING-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.
123456789101112131415161718192021WORKING-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.
DISPLAY writes data to the standard output device (console/terminal):
123DISPLAY identifier-1 [identifier-2 ...] [UPON mnemonic-name] [WITH NO ADVANCING]
1234567891011121314151617181920212223WORKING-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.
READ retrieves records from files:
1234567891011121314151617PROCEDURE 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.
12345678910111213141516PROCEDURE 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.
WRITE adds records to files:
12345678910111213141516PROCEDURE 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.
12345678910111213141516171819PROCEDURE 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.
REWRITE updates existing records:
123456789101112131415161718192021222324PROCEDURE 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.
DELETE removes records from files:
1234567891011121314151617PROCEDURE 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.
Files must be opened before use and closed when done:
12345678910111213141516*> 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
12345678910111213*> 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 indicate operation results:
| Status Code | Meaning | When It Occurs |
|---|---|---|
| '00' | Success | Operation completed successfully |
| '10' | End of file | No more records (sequential read) |
| '23' | Record not found | Key not found (random access) |
| '22' | Duplicate key | Attempt to write duplicate key |
| '30' | Permanent I/O error | Hardware or file system error |
| '24' | Boundary violation | Write beyond file limits |
12345678*> 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"
12345678910*> 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
12345678910*> 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
Follow these best practices:
Think of I/O operations like talking and listening:
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!
Complete these exercises to reinforce your understanding:
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.
Create a program that opens a sequential file, reads all records, and displays each record using DISPLAY. Handle end of file appropriately.
Create a program that gets the current date and time using ACCEPT FROM, formats them nicely, and displays them using DISPLAY.
Create a program that accepts data from the user, writes it to a file using WRITE, and confirms the write was successful using DISPLAY.
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.
1. What statement is used for console input in COBOL?
2. What statement is used for console output in COBOL?
3. What is the difference between ACCEPT and READ?
4. How do you get the current date in COBOL?
5. What is required before using READ or WRITE?
6. What does WITH NO ADVANCING do in DISPLAY?