Processing Files and Records
6.4 Reading Files
Example 6–33 (Cont.) Reading an Indexed File Randomly
DATA DIVISION.
FILE SECTION.
FD
FLAVORS.
01
ICE-CREAM-MASTER.
02 ICE-CREAM-KEY
PIC XXXX.
02 ICE-CREAM-DATA.
03
ICE-CREAM-STORE-CODE
PIC XXXXX.
03
ICE-CREAM-STORE-ADDRESS
PIC X(20).
03
ICE-CREAM-STORE-CITY
PIC X(20).
03
ICE-CREAM-STORE-STATE
PIC XX.
WORKING-STORAGE SECTION.
01
PROGRAM-STAT
PIC X.
88
OPERATOR-STOPS-IT
VALUE "1".
PROCEDURE DIVISION.
A000-BEGIN.
OPEN I-O FLAVORS.
PERFORM A020-INITIAL-PROMPT.
IF OPERATOR-STOPS-IT
PERFORM A005-TERMINATE.
PERFORM A030-RANDOM-READ.
PERFORM A025-SUBSEQUENT-PROMPTS UNTIL OPERATOR-STOPS-IT.
DISPLAY "END OF JOB".
STOP RUN.
A020-INITIAL-PROMPT.
DISPLAY "Do you want to see a store?".
PERFORM A040-GET-ANSWER UNTIL PROGRAM-STAT = "Y" OR "y" OR "N" OR "n".
IF PROGRAM-STAT = "N" OR "n"
MOVE "1" TO PROGRAM-STAT.
A025-SUBSEQUENT-PROMPTS.
MOVE SPACE TO PROGRAM-STAT.
DISPLAY "Do you want to see another store ?".
PERFORM A040-GET-ANSWER UNTIL PROGRAM-STAT = "Y" OR "y" OR "N" OR "n".
IF PROGRAM-STAT = "Y" OR "y"
PERFORM A030-RANDOM-READ
ELSE
MOVE "1" TO PROGRAM-STAT.
A030-RANDOM-READ.
DISPLAY "Enter key".
ACCEPT ICE-CREAM-KEY.
PERFORM A100-READ-INPUT-BY-KEY.
A040-GET-ANSWER.
DISPLAY "Please answer Y or N"
ACCEPT PROGRAM-STAT.
A100-READ-INPUT-BY-KEY.
READ FLAVORS KEY IS ICE-CREAM-KEY
INVALID KEY DISPLAY "Record does not exist - Try again"
NOT INVALID KEY DISPLAY "The record is: ", ICE-CREAM-MASTER.
A005-TERMINATE.
DISPLAY "terminated".
Reading an Indexed File Dynamically
The READ statement has two formats, so it can select the next logical record
(sequential access) or select a specific record (random access) and make it
available to the program. In dynamic mode, the program can switch from using
random access I/O statements to sequential access I/O statements, in any order
and any number of times, without closing and reopening files. However, the
program must use the READ NEXT statement to sequentially read an indexed
file opened in dynamic mode.
6–44 Processing Files and Records
Содержание COBOL AAQ2G1FTK
Страница 22: ......
Страница 30: ......
Страница 94: ......
Страница 110: ......
Страница 146: ......
Страница 180: ......
Страница 194: ...Processing Files and Records 6 1 Defi...
Страница 300: ......
Страница 490: ......
Страница 516: ......
Страница 517: ......
Страница 530: ......
Страница 534: ......
Страница 590: ......
Страница 620: ......