Complete Information on COBOL Indexed files

COBOL Indexed filesThe below is the life cycle of Indexed files in COBOL program:

  • Defining an indexed file
  • Opening and closing indexed files
  • Reading through a file
  • Adding records to indexed files
  • Looking up records in indexed files
  • Changing records in indexed files
  • 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”.

Advertisements