Compaq COBOL AAQ2G1FTK User Manual

Page 225

Advertising
background image

Processing Files and Records

6.4 Reading Files

Sequential processing need not begin at the first record of an indexed file. The
START statement specifies the next record to be read sequentially, selects which
key to use to determine the logical sort order, and repositions the file position
indicator for subsequent I/O operations anywhere within the file.

A sequential read of a dynamic file is indicated by the NEXT phrase of the READ
statement. A READ NEXT statement should follow the START statement since
the READ NEXT statement reads the next record indicated by the file position
indicator. Subsequent READ NEXT statements sequentially retrieve records until
another START statement or random READ statement executes.

Example 6–34 processes an indexed file containing 26 records. Each record has
a unique letter of the alphabet as its primary key. The program positions the file
to the first record whose INPUT-RECORD-KEY is equal to the specified letter of
the alphabet. The program’s READ NEXT statement sequentially retrieves the
remaining valid records in the file for display on the terminal.

Example 6–34 Reading an Indexed File Dynamically

IDENTIFICATION DIVISION.
PROGRAM-ID. INDEX05.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.

SELECT IND-ALPHA

ASSIGN TO "ALPHA"

ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS INPUT-RECORD-KEY.

DATA DIVISION.
FILE SECTION.
FD

IND-ALPHA.

01

INPUT-RECORD.
02

INPUT-RECORD-KEY

PIC X.

02

INPUT-RECORD-DATA

PIC X(50).

WORKING-STORAGE SECTION.
01

END-OF-FILE

PIC X.

PROCEDURE DIVISION.
A000-BEGIN.

OPEN I-O IND-ALPHA.
DISPLAY "Enter letter"
ACCEPT INPUT-RECORD-KEY.
START IND-ALPHA KEY = INPUT-RECORD-KEY

INVALID KEY DISPLAY "BAD START STATEMENT"
NOT INVALID KEY

PERFORM A100-GET-RECORDS THROUGH A100-GET-RECORDS-EXIT

UNTIL END-OF-FILE = "Y" END-START.

A010-END-OF-JOB.

DISPLAY "END OF JOB".
CLOSE IND-ALPHA.
STOP RUN.

Processing Files and Records 6–45

Advertising