READ and AT END
Part of the Files and Reports section of Coddy's COBOL Programming journey. Lesson 3 of 22.
READ copies the next record of the file into the FD record, then moves past it. The first READ gets the first record, the second READ the second.
READ CUST-FILE.When no record is left, a READ has nothing to copy. AT END says what to do then, and NOT AT END what to do with a record that was read:
READ CUST-FILE
AT END DISPLAY "No more customers"
NOT AT END DISPLAY CUST-ID
END-READ.END-READ closes the statement, as END-IF closes an IF.
A statement after END-READ runs either way. In GnuCOBOL the record still holds the last customer at the end of the file, so a DISPLAY there prints that customer again.
Challenge
EasyCUSTIN holds 4 customers and READ-ONE runs 5 times. Write its READ: display CUST-NAME when a record is read, and End of file at the end.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. READEND.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CUST-FILE ASSIGN TO CUSTIN
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD CUST-FILE.
01 CUST-REC.
05 CUST-ID PIC X(4).
05 CUST-NAME PIC X(10).
05 CUST-CITY PIC X(6).
05 CUST-BAL PIC 9(5).
PROCEDURE DIVISION.
MAIN-PARA.
OPEN INPUT CUST-FILE.
PERFORM READ-ONE 5 TIMES.
CLOSE CUST-FILE.
STOP RUN.
READ-ONE.
*> Read CUST-FILE here
All lessons in Files and Reports
Practice on your own: Online COBOL compiler