Sequential files

The previous lesson wrote a file. This one reads it back, and the reading has three states a program must know: a record arrived, the file ended, or the read failed. COBOL shows the first two in the statement and the third in the status field.

OPEN INPUT, READ and AT END

READ takes the file, an AT END branch and an optional NOT AT END branch. One of the two runs for every read, and the record is only valid in the second:

IDENTIFICATION DIVISION. PROGRAM-ID. RW. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT ORD-F ASSIGN TO "orders.dat" ORGANIZATION IS LINE SEQUENTIAL FILE STATUS IS WS-STATUS. DATA DIVISION. FILE SECTION. FD ORD-F. 01 ORD-REC. 05 ORD-ID PIC X(3). 05 ORD-QTY PIC 9(3). WORKING-STORAGE SECTION. 01 WS-STATUS PIC XX. 01 WS-COUNT PIC 9(3) VALUE 0. 01 WS-TOTAL PIC 9(5) VALUE 0. PROCEDURE DIVISION. OPEN OUTPUT ORD-F. MOVE "A10" TO ORD-ID. MOVE 2 TO ORD-QTY. WRITE ORD-REC. MOVE "B20" TO ORD-ID. MOVE 5 TO ORD-QTY. WRITE ORD-REC. MOVE "C30" TO ORD-ID. MOVE 1 TO ORD-QTY. WRITE ORD-REC. CLOSE ORD-F. OPEN INPUT ORD-F. DISPLAY "open " WS-STATUS. PERFORM UNTIL WS-STATUS = "10" READ ORD-F AT END DISPLAY "end of file, status " WS-STATUS NOT AT END ADD 1 TO WS-COUNT ADD ORD-QTY TO WS-TOTAL DISPLAY "read " ORD-ID " qty " ORD-QTY END-READ END-PERFORM. CLOSE ORD-F. DISPLAY "records " WS-COUNT " total " WS-TOTAL. STOP RUN.

cobc -x -free -Wall -o rw rw.cob
./rw

open 00
read A10 qty 002
read B20 qty 005
read C30 qty 001
end of file, status 10
records 003 total 00008

A read loop that ends on the status

The loop tests WS-STATUS and not a flag, so the first read of an empty file never enters it: the status is already 10 when the PERFORM starts and TEST BEFORE checks it first. That is the shape that reads a file of any length, including none.

NOT AT END is optional; a READ with only an AT END branch and no record handling reads and discards, which a counting pass uses. The record is in the FD area after a successful read, and it stays there until the next read changes it, so a program that keeps a record for later must move it out.

An empty file

A file with no records is the case a first read of a new file hits, and the status is 10 on that first read. The program below creates an empty file, opens it for input and reads twice:

IDENTIFICATION DIVISION. PROGRAM-ID. EMPTY2. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT ORD-F ASSIGN TO "empty2.dat" ORGANIZATION IS LINE SEQUENTIAL FILE STATUS IS WS-STATUS. DATA DIVISION. FILE SECTION. FD ORD-F. 01 ORD-REC PIC X(5). WORKING-STORAGE SECTION. 01 WS-STATUS PIC XX. PROCEDURE DIVISION. OPEN OUTPUT ORD-F. CLOSE ORD-F. OPEN INPUT ORD-F. DISPLAY "open " WS-STATUS. READ ORD-F AT END DISPLAY "first read at end" END-READ. DISPLAY "after first " WS-STATUS. READ ORD-F AT END DISPLAY "second read at end" END-READ. DISPLAY "after second " WS-STATUS. CLOSE ORD-F. STOP RUN.

cobc -x -free -Wall -o empty2 empty2.cob
./empty2

open 00
first read at end
after first 10
after second 46

A read after the end

The second read printed nothing and left 46 in the status: the file is at its end, the read has no record to return, and neither branch of the statement ran. 46 is the status of a read that is not allowed in the current position of the file, and it is the reason a loop exits on 10 and not on a count of the reads it made.

A program that keeps reading after the end gets 46, then 46 again, and never another record. The lesson on FILE STATUS reads the codes of this page and the others the language returns as a table; the lesson on OPEN and CLOSE modes covers the modes this program used, and the ones it did not.

Exercises

  1. Change the loop to test a counter against three and run it. What does the fourth read do?
  2. Remove the NOT AT END and put the counting statements directly after END-READ. How many times does the total count the last record?
  3. Open the file for input before the three writes and see what the status says. Which code reports the wrong mode?
  4. Write a second file with the same program and read both in one pass, alternating the two files. Which status ends each one?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does READ do with the record area when it finds the end of the file?

    Answer

    It takes the AT END branch and leaves the area as it was

    The AT END branch runs instead of the record handling, and the area keeps the previous record until the next read.

  2. Which status ends a read loop?

    Answer

    10, the end of the file

    The loop tested WS-STATUS = "10" and stopped after the last record.

  3. What is the status of the first read of an empty file?

    Answer

    10, at end, with no record

    The empty file was already at its end, and the program printed 10 after the first read.

  4. What does a second read after the end return?

    Answer

    Status 46, and neither branch of the read runs

    The second read left 46 in the status and printed nothing at all.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT F-IN ASSIGN TO "q5.dat" ORGANIZATION IS LINE SEQUENTIAL FILE STATUS IS W-S. DATA DIVISION. FILE SECTION. FD F-IN. 01 R-IN PIC X(4). WORKING-STORAGE SECTION. 01 W-S PIC XX. 01 C-F PIC 9 VALUE 0. PROCEDURE DIVISION. OPEN OUTPUT F-IN. MOVE "AAAA" TO R-IN. WRITE R-IN. MOVE "BBBB" TO R-IN. WRITE R-IN. CLOSE F-IN. OPEN INPUT F-IN. PERFORM UNTIL W-S = "10" READ F-IN AT END CONTINUE NOT AT END ADD 1 TO C-F END-READ END-PERFORM. CLOSE F-IN. DISPLAY C-F. STOP RUN.

    Answer

    2

    Two records are read before the end status stops the loop.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q6. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT F-IN ASSIGN TO "q6.dat" ORGANIZATION IS LINE SEQUENTIAL FILE STATUS IS W-S. DATA DIVISION. FILE SECTION. FD F-IN. 01 R-IN PIC X(3). WORKING-STORAGE SECTION. 01 W-S PIC XX. PROCEDURE DIVISION. OPEN OUTPUT F-IN. CLOSE F-IN. OPEN INPUT F-IN. READ F-IN AT END DISPLAY "empty" NOT AT END DISPLAY "record" END-READ. CLOSE F-IN. STOP RUN.

    Answer

    empty

    The file was created with no records, so the first read is already at the end.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q7. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT F-IN ASSIGN TO "q7.dat" ORGANIZATION IS LINE SEQUENTIAL FILE STATUS IS W-S. DATA DIVISION. FILE SECTION. FD F-IN. 01 R-IN PIC X(3). WORKING-STORAGE SECTION. 01 W-S PIC XX. 01 T-F PIC 9(3) VALUE 0. 01 N-F PIC 9(3). PROCEDURE DIVISION. OPEN OUTPUT F-IN. MOVE "010" TO R-IN. WRITE R-IN. MOVE "020" TO R-IN. WRITE R-IN. CLOSE F-IN. OPEN INPUT F-IN. PERFORM UNTIL W-S = "10" READ F-IN AT END CONTINUE NOT AT END MOVE R-IN TO N-F ADD N-F TO T-F END-READ END-PERFORM. CLOSE F-IN. DISPLAY T-F. STOP RUN.

    Answer

    030

    The two records hold 010 and 020, and the addition of the text field converts both to numbers.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 S-F PIC XX VALUE "10". 01 N-F PIC 9 VALUE 0. PROCEDURE DIVISION. IF S-F = "10" MOVE 1 TO N-F ELSE MOVE 2 TO N-F END-IF. DISPLAY N-F. STOP RUN.

    Answer

    1

    The status field holds the end-of-file code, and the comparison with the literal is true.

  9. Why must a program stop reading at status 10?

    Answer

    The next read returns 46 and no branch of the statement runs

    The second read of the page printed nothing and left 46, so a loop that ignores 10 spins on a read that returns no record.

  10. A program keeps the record it read and reads the next one. What happens to the first record?

    Answer

    The record area is overwritten, so the program must move it out first

    One FD area per file holds the last record, and the next READ replaces its bytes.

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

Article author: Arthur Isaev