A batch job often fails before the first business rule runs because the file was opened in the wrong mode. A program that needs to read customer records should not use OPEN OUTPUT. A program that appends audit records should not recreate the file. In COBOL, the OPEN mode tells the runtime what the program plans to do with the file.
What are COBOL file I/O modes?
COBOL file I/O modes are the phrases used with the OPEN statement: INPUT, OUTPUT, I-O, and EXTEND. They decide which file statements are valid after the file is open. If the wrong mode is used, later READ, WRITE, REWRITE, or DELETE statements can fail or damage data.
OPEN INPUT CUSTOMER-FILE
OPEN OUTPUT REPORT-FILE
OPEN I-O MASTER-FILE
OPEN EXTEND AUDIT-FILE
Good file handling is simple: open the file in the mode that matches the next operation, check FILE STATUS, process the records, and close the file before the program ends.
Quick comparison
| OPEN mode | Use it when | Common valid statements | Main risk |
|---|---|---|---|
INPUT |
The program only reads an existing file. | READ |
Fails if a required file is missing. |
OUTPUT |
The program creates a new file or replaces old records. | WRITE |
Can clear existing records when used on the wrong file. |
I-O |
The program reads records and updates them. | READ, REWRITE, DELETE |
Can fail when the organization or access mode does not allow updates. |
EXTEND |
The program appends new records after the last record. | WRITE |
Fails for a missing non-optional file and is not valid for every file type. |
OPEN INPUT: read an existing file
Use OPEN INPUT when the program reads records and does not change the file. A daily balance report, customer extract reader, or validation job usually opens the input file this way.
SELECT CUSTOMER-FILE ASSIGN TO CUSTIN
FILE STATUS IS WS-CUST-STATUS.
OPEN INPUT CUSTOMER-FILE
IF WS-CUST-STATUS NOT = "00"
DISPLAY "CUSTOMER OPEN FAILED: " WS-CUST-STATUS
GOBACK
END-IF
If the required data set is missing, a typical status is 35. If the file is declared OPTIONAL, the open can succeed with a different status, so support teams should check the program's SELECT clause before assuming the JCL is wrong.
OPEN OUTPUT: create or replace records
Use OPEN OUTPUT for a new output file, such as a report, extract, or unload file. Be careful with existing files. On many jobs, opening an existing file for output means the old contents are replaced by new data.
SELECT REPORT-FILE ASSIGN TO REPTDD
FILE STATUS IS WS-REPT-STATUS.
OPEN OUTPUT REPORT-FILE
IF WS-REPT-STATUS = "00"
WRITE REPORT-REC
ELSE
DISPLAY "REPORT OPEN FAILED: " WS-REPT-STATUS
END-IF
This is the mode that deserves the most review in production changes. A wrong DD name or copied program can point OPEN OUTPUT at a valuable file and replace records that were meant to be kept.
OPEN I-O: read and update records
Use OPEN I-O when the same file is read and changed. A VSAM KSDS maintenance program may read a customer master record, change a field, and then issue REWRITE. A delete program may read a key and then issue DELETE.
OPEN I-O CUSTOMER-MASTER
IF WS-MASTER-STATUS NOT = "00"
DISPLAY "MASTER OPEN FAILED: " WS-MASTER-STATUS
GOBACK
END-IF
READ CUSTOMER-MASTER
INVALID KEY DISPLAY "CUSTOMER NOT FOUND"
END-READ
REWRITE CUSTOMER-MASTER-REC
INVALID KEY DISPLAY "REWRITE FAILED"
END-REWRITE
I-O is not a shortcut for every file. The file organization, access mode, and data set allocation must support update processing. If a file is sequential and the program only needs to append new rows, EXTEND is often the clearer choice.
OPEN EXTEND: append new records
Use OPEN EXTEND when the program must keep existing records and add new records at the end. Audit logs, transaction history files, and daily append files often use this mode.
OPEN EXTEND AUDIT-FILE
IF WS-AUDIT-STATUS = "00"
MOVE WS-AUDIT-TEXT TO AUDIT-REC
WRITE AUDIT-REC
ELSE
DISPLAY "AUDIT OPEN FAILED: " WS-AUDIT-STATUS
END-IF
If the file may not exist yet, review whether the file should be defined as OPTIONAL. IBM documents different open results for available and unavailable files, so this choice should be deliberate rather than left to a copied file definition.
File status codes to check after OPEN
Every production COBOL program should check FILE STATUS after OPEN. The exact value depends on file organization and runtime behavior, but these statuses appear often in support work.
| Status | Typical meaning during OPEN | First check |
|---|---|---|
00 |
Open completed normally. | Continue processing. |
05 |
Optional file was not available, but open processing continued. | Confirm whether SELECT OPTIONAL is intended. |
35 |
Required file was not found or not available. | Check the DD statement, catalog entry, and data set name. |
39 |
File attributes do not match the COBOL description. | Compare RECFM, LRECL, keys, and record layout. |
JCL checks before changing the mode
The COBOL source is only half of the story. The DD statement must point to the right data set and allow the intended action. Before changing an open mode, check the production JCL and scheduler variables.
//CUSTIN DD DSN=PROD.CUSTOMER.INPUT,DISP=SHR
//REPTDD DD DSN=PROD.REPORT.DAILY,
// DISP=(NEW,CATLG,DELETE),
// SPACE=(CYL,(5,2)),
// DCB=(RECFM=FB,LRECL=133)
For read-only files, DISP=SHR is common. For new output files, the job often uses DISP=(NEW,CATLG,DELETE). For update jobs, review enqueue rules, restart behavior, and whether another job can read the same data set while updates are running.
Common mistakes
Opening an input file as OUTPUT
This is the dangerous one. A copy-paste change can replace a file that should only be read. Review OPEN OUTPUT statements carefully during code review.
Skipping FILE STATUS after OPEN
If the program does not test the status, the next READ or WRITE may fail far away from the real problem. Log the DD name and status so production support can act quickly.
Using EXTEND when the file is not optional
OPEN EXTEND can fail when a required file is missing. If the first run should create the file, define and test that behavior before the job reaches production.
Practical rule for choosing the mode
- Use
INPUTwhen the program only reads. - Use
OUTPUTwhen the program creates a new result or intentionally replaces old contents. - Use
I-Owhen the program reads existing records and updates or deletes them. - Use
EXTENDwhen the program keeps existing records and appends new ones. - Always check
FILE STATUSimmediately after theOPEN.
Related Mainframe Forum guides
For the next file-handling topics, read COBOL File Operation, COBOL OPEN Statement, COBOL READ Statement, COBOL File Status, COBOL File Organization, and COBOL FD Entries.
External references
IBM documents these details in the Enterprise COBOL OPEN statement, OPEN statement notes, and opening ESDS, KSDS, and RRDS files pages.
FAQ
Which COBOL open mode is used for reading?
Use OPEN INPUT when the program only needs to read records from an existing file.
Which COBOL open mode appends records?
Use OPEN EXTEND when the program needs to add records after the last existing record.
Can COBOL read and update the same file?
Yes, but the file must be opened with OPEN I-O, and the file organization and access mode must support the update operation.
Why is OPEN OUTPUT risky?
OPEN OUTPUT is risky because it can replace existing records. Use it only when the job is meant to create a fresh output file or clear old contents.