Day 16: COBOL Indexed File Handling for Fast Record Access and Efficient Data Updates

 


1. Introduction: Moving from Sequential to Indexed File Organization

In earlier lessons, we worked with sequential files, where data had to be processed one record at a time from start to end. While simple, sequential access becomes inefficient when files grow large — especially if you need to locate or update specific records quickly.

This is where indexed file handling comes in.
Indexed files allow direct access to any record using a key field (like Employee ID or Account Number). This means COBOL can locate, read, or modify a single record without scanning the entire file.

Think of it like a small built-in database — the index acts like a catalog that keeps track of record positions for fast lookups. This approach made COBOL extremely powerful for business systems such as banking, payroll, and retail, where performance and accuracy are crucial.


2. Creating and Using Indexed Files in COBOL

To work with indexed files, we define a KEY field in the FILE SECTION and specify that the file is ORGANIZATION IS INDEXED.

Here’s a complete example that demonstrates how to create, read, and update records in an indexed file.

Program: Indexed Employee File Handling

IDENTIFICATION DIVISION. PROGRAM-ID. INDEXEMP. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT EMPFILE ASSIGN TO "EMPINDEX.DAT" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS EMP-ID FILE STATUS IS WS-FILE-STATUS. DATA DIVISION. FILE SECTION. FD EMPFILE. 01 EMP-RECORD. 05 EMP-ID PIC 9(4). 05 EMP-NAME PIC A(20). 05 EMP-SALARY PIC 9(6)V99. WORKING-STORAGE SECTION. 01 WS-FILE-STATUS PIC XX. 01 WS-CHOICE PIC 9 VALUE 1. 01 WS-SEARCH-ID PIC 9(4). PROCEDURE DIVISION. DISPLAY "CREATING EMPLOYEE INDEXED FILE..." OPEN OUTPUT EMPFILE MOVE 1001 TO EMP-ID MOVE "ALICE" TO EMP-NAME MOVE 50000 TO EMP-SALARY WRITE EMP-RECORD MOVE 1002 TO EMP-ID MOVE "BOB" TO EMP-NAME MOVE 45000 TO EMP-SALARY WRITE EMP-RECORD CLOSE EMPFILE DISPLAY "READING SPECIFIC RECORD..." OPEN I-O EMPFILE MOVE 1002 TO EMP-ID READ EMPFILE KEY IS EMP-ID INVALID KEY DISPLAY "RECORD NOT FOUND" NOT INVALID KEY DISPLAY "FOUND: " EMP-NAME " $" EMP-SALARY END-READ DISPLAY "UPDATING RECORD..." MOVE 1002 TO EMP-ID READ EMPFILE KEY IS EMP-ID INVALID KEY DISPLAY "RECORD NOT FOUND" NOT INVALID KEY MOVE 47000 TO EMP-SALARY REWRITE EMP-RECORD DISPLAY "RECORD UPDATED SUCCESSFULLY" END-READ CLOSE EMPFILE DISPLAY "PROGRAM COMPLETE." STOP RUN.

Explanation:

  • ORGANIZATION IS INDEXED – Defines the file as an indexed file.

  • RECORD KEY IS EMP-ID – Identifies the field used for indexing.

  • ACCESS MODE IS DYNAMIC – Allows both sequential and random access.

  • READ ... KEY IS EMP-ID – Retrieves the record with the specified key.

  • REWRITE – Updates the current record in the file.

  • FILE STATUS – Stores success or error codes after file operations.

Indexed file handling allows you to add, retrieve, or update records instantly using a unique key — similar to how a database uses primary keys.


3. Practical Use Cases and Best Practices

Common Business Scenarios:

  • 🔹 Employee Management Systems: Retrieve or modify data by employee ID.

  • 🔹 Banking Applications: Access specific account numbers quickly.

  • 🔹 Inventory Management: Update product details using product codes.

Best Practices:

  • Always include FILE STATUS to detect errors and avoid crashes.

  • Keep key fields unique to prevent data duplication.

  • Use ACCESS MODE IS DYNAMIC for flexible record handling.

  • Always CLOSE the file properly after read or update operations.

  • Avoid unnecessary REWRITE operations to maintain file integrity.

Indexed files make COBOL a data management powerhouse, enabling fast and precise record manipulation even with large datasets — a reason COBOL still powers critical legacy systems worldwide.


🧠 Key Takeaways

  • Indexed files allow direct access using KEY fields.

  • Use ORGANIZATION IS INDEXED and RECORD KEY to define them.

  • READ ... KEY IS and REWRITE let you modify specific records.

  • FILE STATUS helps track file operation results.

  • Indexed organization enables database-like efficiency in COBOL programs.


💻 Practice Task

  1. Create a program STUINDEX.cbl that:

    • Stores student records (Roll No, Name, Grade) in an indexed file STUFILE.DAT.

    • Allows the user to search for a specific roll number and display details.

  2. Add functionality to update a student’s grade using the REWRITE statement.

  3. Bonus: Implement a feature to delete a record using DELETE and confirm it’s removed.


"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