Day 15: Understanding COBOL File Handling for Sequential Data Processing in Business Applications

 


1. Introduction: Why File Handling Is Crucial in COBOL

File handling is one of the core strengths of COBOL, as it was designed primarily for business data processing. Unlike modern applications that rely heavily on databases, traditional COBOL systems store and process massive amounts of structured data in files — such as payroll data, customer records, or sales logs.

In COBOL, files are divided into records (like rows in a table), and each record can contain multiple fields (like columns). These records can be processed sequentially, meaning the program reads or writes them one by one in order.

Mastering file handling allows COBOL developers to:

  • Create new data files

  • Read existing files record by record

  • Update or rewrite records

  • Generate reports or summaries

COBOL supports various file organization methods — Sequential, Indexed, and Relative — but Sequential File Handling is the simplest and most commonly used for learning and batch processing tasks.


2. How to Define and Process Sequential Files in COBOL

A COBOL program handles files through three major divisions:

  • ENVIRONMENT DIVISION – Defines the file’s external name and location.

  • DATA DIVISION – Defines the record layout.

  • PROCEDURE DIVISION – Contains logic to read, write, or update records.

Here’s a basic example 👇

Program: Writing and Reading Employee Records

IDENTIFICATION DIVISION. PROGRAM-ID. EMPFILE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT EMPFILE ASSIGN TO 'EMPLOYEE.DAT' ORGANIZATION IS SEQUENTIAL. 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-CHOICE PIC 9 VALUE 1. PROCEDURE DIVISION. OPEN OUTPUT EMPFILE MOVE 1001 TO EMP-ID MOVE 'ALICE WILLIAMS' TO EMP-NAME MOVE 55000.75 TO EMP-SALARY WRITE EMP-RECORD CLOSE EMPFILE OPEN INPUT EMPFILE PERFORM UNTIL WS-CHOICE = 0 READ EMPFILE AT END MOVE 0 TO WS-CHOICE NOT AT END DISPLAY "ID: " EMP-ID DISPLAY "NAME: " EMP-NAME DISPLAY "SALARY: " EMP-SALARY END-READ END-PERFORM CLOSE EMPFILE STOP RUN.

Explanation:

  • ASSIGN TO 'EMPLOYEE.DAT' – Links the COBOL file to a system file.

  • FD – File Description defines how each record looks.

  • OPEN OUTPUT – Opens a file for writing (creates if not exists).

  • OPEN INPUT – Opens a file for reading existing records.

  • READ & WRITE – Perform data transfer.

  • CLOSE – Finalizes the file and releases resources.


3. Practical Applications and Best Practices

Sequential file handling forms the foundation of COBOL batch systems. Common examples include:

  • Payroll Systems: Reading employee data to compute salaries.

  • Billing Systems: Writing monthly invoice summaries.

  • Banking Systems: Processing transactions in sequence overnight.

Best Practices:

  • Always use AT END condition in READ loops to avoid runtime errors.

  • Keep file structures consistent across programs.

  • Use separate files for input, processing, and output to simplify debugging.

  • Always close files properly to prevent data corruption.

With proper file handling, COBOL applications can process millions of records efficiently and reliably — a capability still valued in today’s enterprise systems.


🧠 Key Takeaways

  • COBOL files are defined in the ENVIRONMENT and DATA DIVISION.

  • Use OPEN, READ, WRITE, and CLOSE to manage file operations.

  • Sequential organization is simple and ideal for ordered data.

  • Always handle AT END to prevent unexpected file errors.

  • File handling is at the heart of COBOL’s role in batch and transaction systems.


💻 Practice Task

  1. Create a COBOL program STUREC.cbl to:

    • Write 3 student records (ID, Name, Marks) to a file STUDENT.DAT.

    • Read and display all records from the same file.

  2. Add logic to calculate and display the average marks of all students.

  3. Bonus: Add a feature to write only students who passed (marks ≥ 40) into a separate file.



"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