File Handling and I/O Operations
The File Handling domain tests whether you can declare a file correctly and use the right I/O verb for its organization and open mode. Exam items ask which status code appears, why a REWRITE failed, or what ACCESS MODE permits.
FILE-CONTROL and organization
A SELECT entry names the file, assigns it to an external data set or path, and specifies ORGANIZATION (SEQUENTIAL, LINE SEQUENTIAL, INDEXED, RELATIVE), ACCESS MODE (SEQUENTIAL, RANDOM, DYNAMIC), RECORD KEY for indexed files, ALTERNATE RECORD KEY with DUPLICATES, RELATIVE KEY, and FILE STATUS. The FD in the FILE SECTION must match the SELECT name and describe the record layout.
Open modes and verbs
OPEN INPUT allows READ. OPEN OUTPUT creates or truncates the file and allows WRITE. OPEN EXTEND adds records at the end. OPEN I-O allows READ, REWRITE and DELETE. WRITE is valid for OUTPUT and EXTEND, and for indexed/relative files opened I-O. REWRITE and DELETE require a prior successful READ and an I-O file. START positions an indexed or relative file for subsequent sequential reads; it does not retrieve a record.
Status codes
00 success; 02 success with duplicate alternate key; 10 end of file on sequential READ; 22 duplicate key; 23 record not found; 35 file not found on OPEN; 41 file already open; 43 REWRITE/DELETE not allowed; 46 no current record.
Worked example. A program opens an indexed customer file with OPEN INPUT, reads a record, changes a field, and issues REWRITE. The REWRITE fails with status 43 because REWRITE requires OPEN I-O. Correcting the OPEN to I-O and re-reading the record before REWRITE fixes it. If the READ had returned status 23, a following REWRITE would fail with 46 because there is no current record.
Traps
- Using OPEN OUTPUT on an existing master file, which destroys its contents.
- Expecting AT END on a random READ; random reads use INVALID KEY.
- Omitting FILE STATUS and testing only INVALID KEY or AT END.
- Forgetting that CLOSE WITH LOCK prevents reopening in the same run unit.
- Assuming a record area is separate per file; record areas can overlap in storage.