COBOL File Handling Basics: Sequential Files, File Status, and Practical Flow
Learn how COBOL file handling works with SELECT, FD, OPEN, READ, WRITE, CLOSE, and file status checks.
COBOL file handling is one of the first skills every mainframe developer needs. Many batch programs read input files, validate records, apply business rules, and write output files or reports. The structure looks old compared with modern APIs, but the idea is simple: define the file, open it, process records one by one, and close it safely.
Why file handling matters
In enterprise systems, files often carry daily transactions, customer updates, claim records, policy details, or reconciliation data. A small mistake in file handling can create duplicate processing, skipped records, or production abends. That is why COBOL programs usually combine file operations with return-code handling and audit counts.
Core COBOL sections
A typical file program uses:
ENVIRONMENT DIVISIONto map logical file names to JCL DD names.DATA DIVISIONto define the file layout.PROCEDURE DIVISIONto open, read, process, and close the file.
Example pattern:
SELECT INPUT-FILE ASSIGN TO INFILE
ORGANIZATION IS SEQUENTIAL
FILE STATUS IS WS-IN-STATUS.
The DD name INFILE is normally supplied in JCL. This separation allows the same program to run against different physical files without recompiling.
Basic processing flow
A clean batch flow is:
1. Initialize counters and working fields.
2. Open input and output files.
3. Read the first input record.
4. Process until end-of-file.
5. Write valid output records.
6. Close all files.
7. Display processing counts.
In production, counters are not optional. They help support teams confirm how many records were read, processed, rejected, and written.
File status codes
The FILE STATUS clause is important because it gives a two-character status after file operations. Common values:
00= success10= end of file35= file not found or missing allocation39= file attribute mismatch46= read attempted after end of file
A beginner mistake is to check only end-of-file and ignore other statuses. Good programs validate every important operation.
Practical guidance
For production-ready COBOL file handling:
- Always define
FILE STATUS. - Check status after
OPEN,READ,WRITE, andCLOSE. - Keep record counts.
- Separate validation logic from file I/O logic.
- Display meaningful messages before abnormal termination.
Conclusion
COBOL file handling is not just syntax. It is a disciplined batch-processing pattern. Once you understand file definition, file status, and record-loop structure, many mainframe programs become much easier to read and maintain.