COBOL Indexed Files the Best Examples

The below is the list on life cycle of COBOL indexed-files.

  1. Defining an indexed file
  2. Opening and closing indexed files
  3. Reading through a file
  4. Adding records to indexed files
  5. Looking up records in indexed files
  6. Changing records in indexed files
  7. Deleting records in indexed files

Defining indexed file:

DATA DIVISION.
FILE SECTION.
*
FD VENDOR-FILE
LABEL RECORDS ARE STANDARD.
01 VENDOR-RECORD.
05 VENDOR-NUMBER PIC 9(5).
05 VENDOR-NAME PIC X(30).
05 VENDOR-ADDRESS-1 PIC X(30).
05 VENDOR-ADDRESS-2 PIC X(30).
05 VENDOR-CITY PIC X(20).
05 VENDOR-STATE PIC X(2).
05 VENDOR-ZIP PIC X(10).
05 VENDOR-CONTACT PIC X(30).
05 VENDOR-PHONE PIC X(15).

Opening and closing of indexed files:

PROGRAM-BEGIN.
OPEN OUTPUT VENDOR-FILE.
CLOSE VENDOR-FILE.
..
PROCEDURE DIVISION.
PROGRAM-BEGIN.
OPEN I-O VENDOR-FILE.
PERFORM MAIN-PROCESS.
CLOSE VENDOR-FILE.

Reading through a file:

OPEN I-O VENDOR-FILE.
READ VENDOR-FILE NEXT RECORD.

Adding records to indexed files:

WRITE-VENDOR-RECORD.
WRITE VENDOR-RECORD
INVALID KEY
DISPLAY "RECORD ALREADY ON FILE".

Changing Records in Indexed files:

PERFORM ENTER-VENDOR-NUMBER.
PERFORM READ-VENDOR-RECORD.
IF RECORD-FOUND = "Y"
PERFORM LOAD-NEW-VENDOR-DATA
PERFORM REWRITE-VENDOR-RECORD.
*
READ-VENDOR-RECORD.
MOVE "Y" TO RECORD-FOUND.
READ VENDOR-FILE RECORD
INVALID KEY
MOVE "N" TO RECORD-FOUND.
*
REWRITE-VENDOR-RECORD.
REWRITE VENDOR-RECORD
INVALID KEY
DISPLAY "ERROR REWRITING VENDOR RECORD".

Deleting a Record in Indexed files:

PERFORM ENTER-VENDOR-NUMBER.
PERFORM READ-VENDOR-RECORD.
IF RECORD-FOUND = "Y"
PERFORM DELETE-VENDOR-RECORD.
*
READ-VENDOR-RECORD.
MOVE "Y" TO RECORD-FOUND.
READ VENDOR-FILE RECORD
INVALID KEY
MOVE "N" TO RECORD-FOUND.
*
DELETE-VENDOR-RECORD.
DELETE VENDOR-FILE RECORD
INVALID KEY
DISPLAY "ERROR DELETING VENDOR RECORD".

Related Posts

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.