Day 8: Working with Indexed Files in COBOL for Faster Data Access and Efficient Record Management

 

1. Introduction: Why Indexed Files Matter in COBOL

While sequential files are simple and reliable, they require reading through every record to find a specific entry — which becomes inefficient when dealing with large datasets like employee lists or bank accounts. To solve this, COBOL introduced Indexed Files, which use a key field to directly locate records without scanning the entire file.

An Indexed File functions similarly to a database index — it allows fast access, updates, and deletions using a unique key, such as an employee ID or account number. This feature revolutionized COBOL-based business systems, enabling real-time data lookup and efficient transaction processing in banking, insurance, and inventory applications.


2. Defining and Accessing Indexed Files

To use indexed files, you must specify them in the FILE-CONTROL paragraph and define a RECORD KEY.

Example file definition:

ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT EMP-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 EMP-FILE. 01 EMP-RECORD. 05 EMP-ID PIC 9(4). 05 EMP-NAME PIC X(25). 05 EMP-SAL PIC 9(7)V99.

Here’s what’s happening:

  • ORGANIZATION IS INDEXED tells COBOL it’s an indexed file.

  • RECORD KEY IS EMP-ID identifies the field used for lookup.

  • ACCESS MODE IS DYNAMIC allows both sequential and random access.

  • FILE STATUS helps handle file errors gracefully.


3. Reading, Writing, and Searching Indexed Files

Once defined, you can perform operations like WRITE, READ, REWRITE, and DELETE using the key field:

PROCEDURE DIVISION. OPEN OUTPUT EMP-FILE. MOVE 1001 TO EMP-ID. MOVE "PRAVEEN" TO EMP-NAME. MOVE 45000.50 TO EMP-SAL. WRITE EMP-RECORD. CLOSE EMP-FILE. OPEN I-O EMP-FILE. MOVE 1001 TO EMP-ID. READ EMP-FILE KEY IS EMP-ID INVALID KEY DISPLAY "RECORD NOT FOUND" NOT INVALID KEY DISPLAY "EMPLOYEE FOUND: " EMP-NAME EMP-SAL END-READ. CLOSE EMP-FILE. STOP RUN.

This allows you to retrieve records instantly by key, rather than reading all entries sequentially — just like querying a database.


🧠 Key Takeaways

  • Indexed files provide fast, random access to records using a key field.

  • Define indexed files using ORGANIZATION IS INDEXED and RECORD KEY IS.

  • Use READ, WRITE, REWRITE, and DELETE for full record management.

  • The FILE STATUS code helps in handling file operations safely.


💻 Practice Task

  1. Create a COBOL program named EmployeeIndexFile.cbl.

  2. Define an indexed file with fields: Employee ID, Name, and Salary.

  3. Write multiple employee records to the file.

  4. Accept an employee ID from the user and search for the record using the READ KEY IS method.

  5. Display the result or show “Record Not Found.”

Sample outline:

IDENTIFICATION DIVISION. PROGRAM-ID. EMPLOYEE-INDEX-FILE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT EMP-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 EMP-FILE. 01 EMP-RECORD. 05 EMP-ID PIC 9(4). 05 EMP-NAME PIC X(25). 05 EMP-SAL PIC 9(7)V99. WORKING-STORAGE SECTION. 01 WS-FILE-STATUS PIC XX. 01 SEARCH-ID PIC 9(4). PROCEDURE DIVISION. OPEN OUTPUT EMP-FILE. MOVE 1001 TO EMP-ID. MOVE "PRAVEEN" TO EMP-NAME. MOVE 55000.75 TO EMP-SAL. WRITE EMP-RECORD. CLOSE EMP-FILE. OPEN INPUT EMP-FILE. DISPLAY "ENTER EMPLOYEE ID TO SEARCH: ". ACCEPT SEARCH-ID. MOVE SEARCH-ID TO EMP-ID. READ EMP-FILE KEY IS EMP-ID INVALID KEY DISPLAY "RECORD NOT FOUND" NOT INVALID KEY DISPLAY "EMPLOYEE: " EMP-NAME " SALARY: " EMP-SAL END-READ. CLOSE EMP-FILE. STOP RUN.


"This Content Sponsored by SBO Digital Marketing.

Mobile-Based Part-Time Job Opportunity by SBO!

Earn money online by doing simple content publishing and sharing tasks. Here's how:

  • Job Type: Mobile-based part-time work
  • Work Involves:
    • Content publishing
    • Content sharing on social media
  • Time Required: As little as 1 hour a day
  • Earnings: ₹300 or more daily
  • Requirements:
    • Active Facebook and Instagram account
    • Basic knowledge of using mobile and social media

For more details:

WhatsApp your Name and Qualification to 9994104160

a.Online Part Time Jobs from Home

b.Work from Home Jobs Without Investment

c.Freelance Jobs Online for Students

d.Mobile Based Online Jobs

e.Daily Payment Online Jobs

Keyword & Tag: #OnlinePartTimeJob #WorkFromHome #EarnMoneyOnline #PartTimeJob #jobs #jobalerts #withoutinvestmentjob"


Comments