COBOL file I/O is how your program reads from and writes to files (datasets on the mainframe or files elsewhere). You declare the file in the ENVIRONMENT and DATA divisions, open it in a mode that matches what you will do (read, write, or update), then use READ, WRITE, REWRITE, or DELETE as appropriate, and close the file when done. Every operation can set a file status code so your program can handle success, end-of-file, and errors. This page covers the basic flow: SELECT and FD, OPEN modes, READ and WRITE, REWRITE and DELETE, CLOSE, and file status.
File I/O is reading from and writing to a file. The file is like a list of records (lines) on disk. Before you can read or write, you "open" the file and say what you will do: only read, only write new stuff, or read and change. Then you read one record at a time (or write new ones). When you read, you might reach the end of the list—that is "end of file." When you are done, you "close" the file. The system also gives you a short code after each action (success, end of file, or error) so your program can decide what to do next.
In the ENVIRONMENT DIVISION, FILE-CONTROL section, you use SELECT to associate a logical file name with an external file (ASSIGN) and optionally a file status data item. The file status item (e.g. PIC X(2)) is set after each I/O operation so you can check the result. In the DATA DIVISION, FILE SECTION, you define the file with an FD (file description) and the record layout(s) that describe each record. The record area is where the current record is read into or where you build a record before WRITE. Only one record format is "current" at a time for a given file.
1234567891011121314151617ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUST-FILE ASSIGN TO CUSTDS ORGANIZATION IS SEQUENTIAL FILE STATUS IS WS-FILE-STATUS. DATA DIVISION. FILE SECTION. FD CUST-FILE. 01 CUST-RECORD. 05 CUST-ID PIC 9(6). 05 CUST-NAME PIC X(30). 05 CUST-AMT PIC S9(7)V99. WORKING-STORAGE SECTION. 01 WS-FILE-STATUS PIC X(2).
OPEN connects your program to the file and sets the mode. You must OPEN before any READ, WRITE, REWRITE, or DELETE. The mode determines what operations are allowed. OPEN INPUT is for reading only: you can only READ. OPEN OUTPUT creates a new file (or replaces an existing one) and allows only WRITE. OPEN I-O opens an existing file for both reading and updating: you can READ, then REWRITE the same record or DELETE it (for indexed files). OPEN EXTEND opens an existing file so you can only WRITE new records at the end (append). If you open a required file that does not exist (e.g. INPUT or I-O), you get a status such as 35 (file not found). If you open OUTPUT and the file exists, it is deleted and recreated empty.
| Mode | Purpose | When to use |
|---|---|---|
| INPUT | Read only | Existing file; READ only |
| OUTPUT | Write only; create new | New file; WRITE only; deletes existing file |
| I-O | Read and update | Existing file; READ, REWRITE, DELETE |
| EXTEND | Append | Existing file; WRITE at end only |
123456789OPEN INPUT CUST-FILE *> or: OPEN OUTPUT OUT-FILE *> or: OPEN I-O MASTER-FILE *> or: OPEN EXTEND LOG-FILE IF WS-FILE-STATUS NOT = '00' AND WS-FILE-STATUS NOT = '05' DISPLAY 'OPEN failed ' WS-FILE-STATUS STOP RUN END-IF
READ gets the next record (sequential) or a record by key (indexed/relative). For sequential files, READ advances through the file; each READ returns the next record. You use AT END (or check file status 10) to detect when there are no more records. For indexed or relative files, you can READ with a key to get a specific record, or READ NEXT to get the next in key order after a successful START or READ. After a successful READ, the record is in the FD record area. Always check file status (or AT END / INVALID KEY) before using the record; if status is not 00, the record area may be undefined.
123456789101112*> Sequential read loop PERFORM UNTIL WS-FILE-STATUS = '10' READ CUST-FILE AT END SET WS-EOF TO TRUE NOT AT END PERFORM PROCESS-RECORD END-READ IF WS-FILE-STATUS NOT = '00' AND WS-FILE-STATUS NOT = '10' DISPLAY 'READ error ' WS-FILE-STATUS EXIT PERFORM END-IF END-PERFORM
WRITE adds a new record. For sequential files, the record is written at the current position (next after the last write or at the start for OUTPUT). For indexed files, the record is inserted by key. Use WRITE for files opened OUTPUT or EXTEND. REWRITE replaces the last record that was read (or the record at the current key for indexed). You must have successfully read the record before REWRITE. Use REWRITE for files opened I-O when you have modified the record in the record area and want to write it back. For indexed files, DELETE removes the last record read (or the record at the current key). After REWRITE or DELETE, the record is no longer "current" until you read again.
12345678910111213MOVE new-data TO CUST-RECORD WRITE CUST-RECORD IF WS-FILE-STATUS NOT = '00' DISPLAY 'WRITE error ' WS-FILE-STATUS END-IF *> Update: after READ, change and REWRITE READ CUST-FILE ... *> modify CUST-RECORD REWRITE CUST-RECORD IF WS-FILE-STATUS NOT = '00' DISPLAY 'REWRITE error ' WS-FILE-STATUS END-IF
CLOSE releases the file. You should CLOSE every file you opened before the program ends. After CLOSE, no further I/O is allowed on that file until you OPEN it again. If you do not CLOSE, the system may close it at program termination, but explicit CLOSE is good practice and ensures buffers are flushed. Check file status after CLOSE; 00 means success.
The two-byte file status is set after every OPEN, READ, WRITE, REWRITE, DELETE, START, and CLOSE. The first byte is a category: 0 = success, 1 = AT END, 2 = invalid key, 3 = permanent error, 4 = logic error, 9 = implementer. The second byte gives detail. Common codes: 00 success, 10 end of file, 21 sequence error, 23 record not found, 30 permanent error, 35 file not found at OPEN. Always check file status after each operation and branch accordingly; do not assume success.
| Code | Meaning | Typical action |
|---|---|---|
| 00 | Success | Continue |
| 10 | End of file (AT END) | Exit read loop |
| 21 | Sequence error (sequential) | Check key order |
| 23 | Record not found (keyed) | Handle missing record |
| 30 | Permanent error | Do not retry; log and recover |
| 35 | File not found at OPEN | Check file name / dataset |
Sequential files are read and written in physical order. You OPEN, then READ in a loop until AT END, or WRITE records one after another. Indexed files (and relative files) have keys: you can READ by key, START at a key, READ NEXT, REWRITE, and DELETE. The SELECT and FD must specify ORGANIZATION IS INDEXED (or RELATIVE) and the key(s). Invalid key conditions (duplicate key, record not found) set status 2x. For more detail, see the sequential-file-operations and file-organization tutorials.
1. To update a record in place you must:
2. File status 00 after a READ means:
3. OPEN OUTPUT on an existing file: