Processing Files and Records
6.4 Reading Files
available to the program. In dynamic mode, the program can switch from random
access I/O statements to sequential access I/O statements in any order, without
closing and reopening files. However, you must use the READ NEXT statement
to sequentially read a relative file open in dynamic mode.
Sequential processing need not begin at the first record of a relative file. The
START statement repositions the file position indicator for subsequent I/O
operations.
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 current record
pointer. Subsequent READ NEXT statements sequentially retrieve records until
another START statement or random READ statement executes.
Example 6–31 processes a relative file containing 10 records. If the previous
program examples in this chapter have been run, each record has a unique even
number from 2 to 20 as its key. The program positions the record pointer (using
the START statement) to the cell corresponding to the value in INPUT-RECORD-
KEY. The program’s READ...NEXT statement retrieves the remaining valid
records in the file for display on the terminal.
Example 6–31 Reading a Relative File Dynamically
IDENTIFICATION DIVISION.
PROGRAM-ID. REL06.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FLAVORS ASSIGN TO "BRAND"
ORGANIZATION IS RELATIVE
ACCESS MODE IS DYNAMIC
RELATIVE KEY IS KETCHUP-MASTER-KEY.
DATA DIVISION.
FILE SECTION.
FD
FLAVORS.
01
KETCHUP-MASTER
PIC X(50).
WORKING-STORAGE SECTION.
01
KETCHUP-MASTER-KEY
PIC 99.
01
END-OF-FILE
PIC X
VALUE "N".
PROCEDURE DIVISION.
A000-BEGIN.
OPEN I-O FLAVORS.
DISPLAY "Enter number".
ACCEPT KETCHUP-MASTER-KEY.
START FLAVORS KEY = KETCHUP-MASTER-KEY
INVALID KEY DISPLAY "Bad START statement"
GO TO A005-END-OF-JOB.
PERFORM A010-DISPLAY-RECORDS UNTIL END-OF-FILE = "Y".
A005-END-OF-JOB.
DISPLAY "END OF JOB".
CLOSE FLAVORS.
STOP RUN.
A010-DISPLAY-RECORDS.
READ FLAVORS NEXT RECORD AT END MOVE "Y" TO END-OF-FILE.
IF END-OF-FILE NOT = "Y" DISPLAY KETCHUP-MASTER.
Processing Files and Records 6–41
Содержание COBOL AAQ2G1FTK
Страница 22: ......
Страница 30: ......
Страница 94: ......
Страница 110: ......
Страница 146: ......
Страница 180: ......
Страница 194: ...Processing Files and Records 6 1 Defi...
Страница 300: ......
Страница 490: ......
Страница 516: ......
Страница 517: ......
Страница 530: ......
Страница 534: ......
Страница 590: ......
Страница 620: ......