Module lessons (2/2)
File I/O Operations
Once a file has been associated and declared in the DATA DIVISION, we can manipulate it inside the PROCEDURE DIVISION. In COBOL, managing the lifecycle of files follows a standard, imperative flow: open, perform operations (read or write), and close.
1. Opening Files: The OPEN Statement
Before you can read from or write to a file, it must be opened by specifying the appropriate access mode:
OPEN INPUT SALES-FILE.
The main modes are:
INPUT: The file is opened in read-only mode. The file must exist on disk, otherwise the program will fail.OUTPUT: The file is opened in write mode. If the file already exists, its previous content is deleted (overwrite). If it does not exist, it is created.EXTEND: The file is opened to append data. New records will be written starting from the end of the existing file.I-O: The file is opened for both reading and writing (mostly used for relative or indexed access files, not linear sequential files).
2. Reading Data: The READ Statement
To read a record from a sequential file, we use the READ statement. Since we do not know in advance how many records the file contains, we must always handle the end of file (EOF) condition using the AT END clause:
READ SALES-FILE
AT END MOVE "Y" TO WS-EOF
END-READ.
READ SALES-FILE: Note that the statement references the logical file name (defined in theSELECTandFDclauses), not the record name.AT END: Specifies the instructions to run when you attempt to read past the last available record. Typically, you set a flag variable (likeWS-EOF) to"Y".END-READ: Explicitly terminates the read statement.
3. Writing Data: The WRITE Statement
To insert data into a file, we use the WRITE statement.
[!WARNING] In COBOL, you read a file (
READ SALES-FILE), but you write a record (WRITE SALES-RECORD). WritingWRITE SALES-FILEis a common syntax error that will prevent compilation.
MOVE 1001 TO EMP-ID.
MOVE "MARIO ROSSI" TO EMP-NAME.
WRITE SALES-RECORD.
Before executing the WRITE statement, the subordinate fields of the FD record structure (in this case, SALES-RECORD) must be populated in memory.
4. Closing Files: The CLOSE Statement
When processing is complete, every open file must be closed to release system resources and ensure all data is written to disk:
CLOSE SALES-FILE.
Note that the CLOSE statement takes the logical file name as its argument, not the record name.
Try it out
Complete the PROCEDURE DIVISION by opening SALES-FILE in INPUT mode, reading the first record, and setting the WS-EOF control variable to 'Y' when the end of the file is reached. Finally, remember to close the file before stopping.
Show hint
Write in order: OPEN INPUT SALES-FILE. then READ SALES-FILE AT END MOVE 'Y' TO WS-EOF END-READ. then CLOSE SALES-FILE. and finally STOP RUN.
Solution available after 3 attempts
Complete the PROCEDURE DIVISION by opening SALES-FILE in OUTPUT mode. Move the value 1001 to SALES-ID and 250.50 to SALES-AMOUNT, then write the SALES-RECORD to the file. Finally, close the file before stopping the program.
Show hint
Open the file with OPEN OUTPUT SALES-FILE., use MOVE to assign values to SALES-ID and SALES-AMOUNT, execute WRITE SALES-RECORD. (writing the record, not the file!), close with CLOSE SALES-FILE. and terminate with STOP RUN.
Solution available after 3 attempts