Processing Files and Records
6.4 Reading Files
Example 6–32 (Cont.) Reading an Indexed File Sequentially
PROCEDURE DIVISION.
A000-BEGIN.
OPEN INPUT FLAVORS.
A010-SEQUENTIAL-READ.
PERFORM A100-READ-INPUT UNTIL END-OF-FILE = "Y".
A020-EOJ.
DISPLAY "END OF JOB".
STOP RUN.
A100-READ-INPUT.
READ FLAVORS AT END MOVE "Y" TO END-OF-FILE.
IF END-OF-FILE NOT = "Y"
DISPLAY ICE-CREAM-MASTER
STOP "Type CONTINUE to display next master".
Reading an Indexed File Randomly
Reading indexed records randomly involves the following:
1. Specifying ORGANIZATION IS INDEXED in the Environment Division
SELECT clause
2. Specifying ACCESS MODE IS RANDOM in the Environment Division
SELECT clause
3. Opening the file for INPUT or I-O
4. Initializing the RECORD KEY or ALTERNATE RECORD KEY data name
before reading the record
5. Reading the record using the KEY IS clause
To read the file randomly, the program must initialize either the primary key
data name or the alternate key data name before reading the target record, and
specify that data name in the KEY IS phrase of the READ statement.
The READ statement selects a specific record from an open file and makes it
available to the program. The value of the primary or alternate key identifies
the specific record. The system randomly reads the record identified by the KEY
clause. If the I/O system does not find a valid record, the invalid key condition
occurs, and the READ statement fails (see Chapter 7).
Example 6–33 reads an indexed file randomly, displaying its contents on the
terminal.
Example 6–33 Reading an Indexed File Randomly
IDENTIFICATION DIVISION.
PROGRAM-ID. INDEX04.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FLAVORS ASSIGN TO "DAIRY"
ORGANIZATION IS INDEXED
ACCESS MODE IS RANDOM
RECORD KEY IS ICE-CREAM-KEY.
(continued on next page)
Processing Files and Records 6–43