Sequential files: READ and WRITE

OPEN and CLOSE

OPEN prepares the file with a mode: INPUT for reading, OUTPUT for a new file, EXTEND to append, and I-O for update. CLOSE releases it:

OPEN INPUT EMPLOYEE-FILE. CLOSE EMPLOYEE-FILE.

READ AT END

READ fetches the next record into the FD record area. AT END runs the end-of-file sentence, and NOT AT END runs when a record arrived. END-READ closes the scope:

READ EMPLOYEE-FILE AT END MOVE "Y" TO WS-EOF NOT AT END PERFORM PROCESS-ROW END-READ.

WRITE and REWRITE

WRITE puts the record area into an OUTPUT or EXTEND file. On an I-O file, REWRITE replaces the record you last READ, and DELETE removes it:

OPEN OUTPUT EMPLOYEE-FILE. MOVE "Ada Lovelace" TO EMP-NAME. WRITE EMP-RECORD.

The code on this page was run with GnuCOBOL 3.2.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev