Cobol Index Files
|
Indexed File I/O
An indexed file is defined both logically and physically. The logical
description is an FD in the FILE SECTION of the DATA DIVISION, as shown
below:
Logical file description 001600 DATA DIVISION. 001700 FILE SECTION. 001800 001900 FD VENDOR-FILE 002000 LABEL RECORDS ARE STANDARD. 002100 01 VENDOR-RECORD. 002200 05 VENDOR-NUMBER PIC 9(5). 002300 05 VENDOR-NAME PIC X(30). 002400 05 VENDOR-ADDRESS-1 PIC X(30). 002500 05 VENDOR-ADDRESS-2 PIC X(30). 002600 05 VENDOR-CITY PIC X(20). 002700 05 VENDOR-STATE PIC X(2). 002800 05 VENDOR-ZIP PIC X(10). 002900 05 VENDOR-CONTACT PIC X(30). 003000 05 VENDOR-PHONE PIC X(15).
Physical description of an indexed file
000600 ENVIRONMENT DIVISION. 000700 INPUT-OUTPUT SECTION. 000800 FILE-CONTROL. 000900 001000 SELECT VENDOR-FILE 001100 ASSIGN TO "VENDOR" 001200 ORGANIZATION IS INDEXED 001300 RECORD KEY IS VENDOR-NUMBER 001400 ACCESS MODE IS DYNAMIC. 001500Remember that any part of a COBOL program can be written in a separate file and included in the main file by using a COPY statement in the main file. Although you can do this with any part of a program, it is common to do it with the SELECT and FD of a file. By using a COPY statement in any program that uses that file, you can ensure that each program has defined the file in the same way. Using COPY 000600 ENVIRONMENT DIVISION. 000700 INPUT-OUTPUT SECTION. 000800 FILE-CONTROL. 000900 001000 COPY "SLVND01.CBL". 001100 001200 DATA DIVISION. 001300 FILE SECTION. 001400 001500 COPY "FDVND01.CBL". 001600Opening and Closing Indexed Files You open an indexed file by specifying the open mode and the filename. Output mode creates a new file; I/O mode allows reading and writing access to an existing file. You close a file by naming the file after a CLOSE command. 003400 PROCEDURE DIVISION. 003500 PROGRAM-BEGIN. 003600 OPEN I-O VENDOR-FILE. 003700 PERFORM MAIN-PROCESS. 003800 CLOSE VENDOR-FILE. |
Reading Through a File
When an indexed file is opened in I/O or input mode, the file is
usually set up so that a READ NEXT on the file retrieves the record
with the lowest primary key value. The READ NEXT action either reads
the record with the primary key of the lowest value, or it produces an
error if no records are in the file.
Adding Records to Indexed Files
Reading the record with the lowest primary key 003600 OPEN I-O VENDOR-FILE. 003700 READ VENDOR-FILE NEXT RECORD.Trapping AT END 003600 READ-NEXT-VENDOR-RECORD. 003700 READ VENDOR-FILE NEXT RECORD 003800 AT END MOVE "Y" TO END-OF-FILE.
Records are added to the file by loading the values in the record and
using a WRITE command followed by the name of the record. The INVALID
KEY condition should be used to trap write errors.
Looking Up Records in Indexed Files
005100 WRITE-VENDOR-RECORD. 005200 WRITE VENDOR-RECORD 005300 INVALID KEY 005400 DISPLAY "RECORD ALREADY ON FILE".
A record can be read from an indexed file by moving a value to the field
that is the primary key of the file and using READ.
The INVALID KEY condition should be used to trap file error conditions
that happen when a record does not exist that matches the value
requested in the primary key field.
Changing Records in Indexed Files
019800 PERFORM ENTER-VENDOR-NUMBER. 019900 MOVE "Y" TO RECORD-FOUND. 020000 READ VENDOR-FILE RECORD 020100 INVALID KEY 020200 MOVE "N" TO RECORD-FOUND. 020300
You can change a record in an indexed file by reading the original
record, loading the new values into the fields of the record, and then
using a REWRITE command followed by the name of the record.
The primary key of the record cannot be changed using this method, but
any other field in the record can be changed.
019200 PERFORM ENTER-VENDOR-NUMBER. 019300 PERFORM READ-VENDOR-RECORD. 019400 IF RECORD-FOUND = "Y" 019500 PERFORM LOAD-NEW-VENDOR-DATA 019600 PERFORM REWRITE-VENDOR-RECORD. 019700 019800 READ-VENDOR-RECORD. 019900 MOVE "Y" TO RECORD-FOUND. 020000 READ VENDOR-FILE RECORD 020100 INVALID KEY 020200 MOVE "N" TO RECORD-FOUND. 020300 020400 REWRITE-VENDOR-RECORD. 020500 REWRITE VENDOR-RECORD 020600 INVALID KEY 020700 DISPLAY "ERROR REWRITING VENDOR RECORD". |

