Indexed file processing in COBOL allows for efficient data retrieval and manipulation by using keys to access records. This method is particularly useful for applications that require frequent searches, updates, and deletions of records.
Key Concepts
- Indexed Files: Files that use one or more keys to access records.
- Primary Key: A unique key that identifies each record.
- Alternate Key: Additional keys that can be used to access records.
- Record: A collection of related data fields.
- File Organization: The way records are stored and accessed in a file.
Structure of an Indexed File
An indexed file in COBOL is defined in the FILE SECTION of the DATA DIVISION. The structure includes the file description (FD) and the record description (01 level).
Example
DATA DIVISION. FILE SECTION. FD EMPLOYEE-FILE LABEL RECORDS ARE STANDARD BLOCK CONTAINS 0 RECORDS RECORD CONTAINS 80 CHARACTERS DATA RECORD IS EMPLOYEE-RECORD. 01 EMPLOYEE-RECORD. 05 EMP-ID PIC X(10). 05 EMP-NAME PIC X(30). 05 EMP-DEPARTMENT PIC X(20). 05 EMP-SALARY PIC 9(8)V99.
Explanation
- FD EMPLOYEE-FILE: Defines the file.
- LABEL RECORDS ARE STANDARD: Specifies standard label records.
- BLOCK CONTAINS 0 RECORDS: Indicates no blocking.
- RECORD CONTAINS 80 CHARACTERS: Specifies the length of each record.
- DATA RECORD IS EMPLOYEE-RECORD: Names the data record.
COBOL Statements for Indexed File Processing
- SELECT Statement
The SELECT statement associates a file with a file control entry.
SELECT EMPLOYEE-FILE ASSIGN TO 'EMPLOYEE.DAT' ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS EMP-ID FILE STATUS IS WS-FILE-STATUS.
Explanation
- ASSIGN TO 'EMPLOYEE.DAT': Specifies the physical file name.
- ORGANIZATION IS INDEXED: Indicates the file is indexed.
- ACCESS MODE IS DYNAMIC: Allows for both sequential and random access.
- RECORD KEY IS EMP-ID: Defines the primary key.
- FILE STATUS IS WS-FILE-STATUS: Stores the file status code.
- OPEN Statement
The OPEN statement opens the file for processing.
Explanation
- I-O: Opens the file for input and output operations.
- READ Statement
The READ statement retrieves records from the file.
Explanation
- NEXT RECORD: Reads the next record sequentially.
- AT END: Executes if the end of the file is reached.
- NOT AT END: Executes if a record is successfully read.
- WRITE Statement
The WRITE statement adds new records to the file.
Explanation
- WRITE EMPLOYEE-RECORD: Writes the current record to the file.
- REWRITE Statement
The REWRITE statement updates existing records.
Explanation
- REWRITE EMPLOYEE-RECORD: Updates the current record in the file.
- DELETE Statement
The DELETE statement removes records from the file.
Explanation
- DELETE EMPLOYEE-RECORD: Deletes the current record from the file.
- CLOSE Statement
The CLOSE statement closes the file after processing.
Explanation
- CLOSE EMPLOYEE-FILE: Closes the file.
Practical Example
COBOL Program to Demonstrate Indexed File Processing
IDENTIFICATION DIVISION. PROGRAM-ID. IndexedFileExample. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT EMPLOYEE-FILE ASSIGN TO 'EMPLOYEE.DAT' ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS EMP-ID FILE STATUS IS WS-FILE-STATUS. DATA DIVISION. FILE SECTION. FD EMPLOYEE-FILE. 01 EMPLOYEE-RECORD. 05 EMP-ID PIC X(10). 05 EMP-NAME PIC X(30). 05 EMP-DEPARTMENT PIC X(20). 05 EMP-SALARY PIC 9(8)V99. WORKING-STORAGE SECTION. 01 WS-FILE-STATUS PIC XX. 01 WS-EOF PIC X VALUE 'N'. PROCEDURE DIVISION. OPEN I-O EMPLOYEE-FILE IF WS-FILE-STATUS NOT = '00' DISPLAY 'Error opening file' STOP RUN END-IF. PERFORM UNTIL WS-EOF = 'Y' READ EMPLOYEE-FILE NEXT RECORD AT END MOVE 'Y' TO WS-EOF NOT AT END DISPLAY EMPLOYEE-RECORD END-READ END-PERFORM. CLOSE EMPLOYEE-FILE. STOP RUN.
Explanation
- IDENTIFICATION DIVISION: Identifies the program.
- ENVIRONMENT DIVISION: Specifies the environment.
- FILE-CONTROL: Associates the file with a file control entry.
- DATA DIVISION: Defines the file and working storage.
- PROCEDURE DIVISION: Contains the logic for file processing.
Exercises
Exercise 1: Create and Write to an Indexed File
- Define an indexed file with the following fields: Student-ID, Student-Name, Course, and Grade.
- Write a COBOL program to create the file and add five records.
Solution
IDENTIFICATION DIVISION. PROGRAM-ID. CreateIndexedFile. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT STUDENT-FILE ASSIGN TO 'STUDENT.DAT' ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS STUDENT-ID FILE STATUS IS WS-FILE-STATUS. DATA DIVISION. FILE SECTION. FD STUDENT-FILE. 01 STUDENT-RECORD. 05 STUDENT-ID PIC X(10). 05 STUDENT-NAME PIC X(30). 05 COURSE PIC X(20). 05 GRADE PIC 9(2). WORKING-STORAGE SECTION. 01 WS-FILE-STATUS PIC XX. PROCEDURE DIVISION. OPEN OUTPUT STUDENT-FILE IF WS-FILE-STATUS NOT = '00' DISPLAY 'Error opening file' STOP RUN END-IF. MOVE 'S001' TO STUDENT-ID MOVE 'John Doe' TO STUDENT-NAME MOVE 'Mathematics' TO COURSE MOVE 85 TO GRADE WRITE STUDENT-RECORD. MOVE 'S002' TO STUDENT-ID MOVE 'Jane Smith' TO STUDENT-NAME MOVE 'Science' TO COURSE MOVE 90 TO GRADE WRITE STUDENT-RECORD. MOVE 'S003' TO STUDENT-ID MOVE 'Alice Johnson' TO STUDENT-NAME MOVE 'History' TO COURSE MOVE 88 TO GRADE WRITE STUDENT-RECORD. MOVE 'S004' TO STUDENT-ID MOVE 'Bob Brown' TO STUDENT-NAME MOVE 'Geography' TO COURSE MOVE 92 TO GRADE WRITE STUDENT-RECORD. MOVE 'S005' TO STUDENT-ID MOVE 'Charlie Davis' TO STUDENT-NAME MOVE 'Physics' TO COURSE MOVE 87 TO GRADE WRITE STUDENT-RECORD. CLOSE STUDENT-FILE. STOP RUN.
Exercise 2: Read and Display Records from an Indexed File
- Write a COBOL program to read and display all records from the indexed file created in Exercise 1.
Solution
IDENTIFICATION DIVISION. PROGRAM-ID. ReadIndexedFile. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT STUDENT-FILE ASSIGN TO 'STUDENT.DAT' ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS STUDENT-ID FILE STATUS IS WS-FILE-STATUS. DATA DIVISION. FILE SECTION. FD STUDENT-FILE. 01 STUDENT-RECORD. 05 STUDENT-ID PIC X(10). 05 STUDENT-NAME PIC X(30). 05 COURSE PIC X(20). 05 GRADE PIC 9(2). WORKING-STORAGE SECTION. 01 WS-FILE-STATUS PIC XX. 01 WS-EOF PIC X VALUE 'N'. PROCEDURE DIVISION. OPEN I-O STUDENT-FILE IF WS-FILE-STATUS NOT = '00' DISPLAY 'Error opening file' STOP RUN END-IF. PERFORM UNTIL WS-EOF = 'Y' READ STUDENT-FILE NEXT RECORD AT END MOVE 'Y' TO WS-EOF NOT AT END DISPLAY STUDENT-RECORD END-READ END-PERFORM. CLOSE STUDENT-FILE. STOP RUN.
Common Mistakes and Tips
- File Status Check: Always check the file status after opening, reading, writing, or closing a file to handle errors gracefully.
- Key Management: Ensure that the primary key is unique for each record to avoid conflicts.
- File Organization: Understand the file organization and access mode to optimize performance.
Conclusion
Indexed file processing in COBOL is a powerful feature that allows for efficient data management using keys. By understanding the structure and statements involved, you can create, read, update, and delete records in an indexed file. Practice with the provided exercises to reinforce your understanding and become proficient in indexed file processing.