Processing Files and Records
6.4 Reading Files
Example 6–28 Reading a Sequential File
IDENTIFICATION DIVISION.
PROGRAM-ID. SEQ02.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT TRANS-FILE ASSIGN TO "TRANS".
DATA DIVISION.
FILE SECTION.
FD TRANS-FILE.
01 TRANSACTION-RECORD PIC X(25).
PROCEDURE DIVISION.
A000-BEGIN.
OPEN INPUT TRANS-FILE.
PERFORM A100-READ-TRANS-FILE
UNTIL TRANSACTION-RECORD = "END".
CLOSE TRANS-FILE.
STOP RUN.
A100-READ-TRANS-FILE.
READ TRANS-FILE
AT END MOVE "END" TO TRANSACTION-RECORD.
IF TRANSACTION-RECORD NOT = "END"
DISPLAY TRANSACTION-RECORD.
6.4.2 Reading a Relative File
Your program can read a relative file sequentially, randomly, or dynamically. The
following three sections describe the specific tasks involved in reading a relative
file sequentially, randomly, and dynamically.
Reading a Relative File Sequentially
Reading relative records sequentially involves the following:
1. Specifying ORGANIZATION IS RELATIVE in the Environment Division
SELECT clause
2. Specifying ACCESS MODE IS SEQUENTIAL (or DYNAMIC) in the
Environment Division SELECT clause (and using the READ NEXT phrase)
3. Opening the file for INPUT or I-O
4. Reading records as you would a sequential file, or using a START statement
The READ statement makes the next logical record of an open file available to the
program. The system reads the file sequentially from either cell 1 or wherever
you START the file, up to cell n. It skips the empty cells and retrieves only valid
records. Each READ statement updates the contents of the file’s RELATIVE
KEY data item, if specified. The data item contains the relative number of the
available record. When the at end condition occurs, execution of the READ
statement is unsuccessful (see Chapter 7).
Sequential processing need not begin at the first record of a relative file. The
START statement specifies the next record to be read and positions the file
position indicator for subsequent I/O operations.
Example 6–29 reads a relative file sequentially, displaying every record on the
terminal.
6–38 Processing Files and Records