Day 7: Understanding File Handling in COBOL for Reading, Writing, and Managing Sequential Business Data Files

 


1. Introduction: The Role of File Handling in COBOL

File handling is one of COBOL’s strongest and most widely used features. In fact, COBOL was designed to process large volumes of business data stored in files — from payroll records to banking transactions. Unlike many modern languages that rely on databases, COBOL traditionally works with sequential files, where data is stored in a fixed order and accessed line by line.

COBOL’s file handling structure ensures data accuracy and reliability, allowing businesses to manage millions of records efficiently. Understanding how COBOL reads, writes, and updates files is essential for building real-world enterprise applications.


2. File Handling Structure and Key Statements

In COBOL, every program that deals with files must define them in the ENVIRONMENT DIVISION and DATA DIVISION before use. The process involves three key steps:

  • OPEN – To open a file for input, output, or update.

  • READ / WRITE / REWRITE / DELETE – To perform file operations.

  • CLOSE – To close the file after processing.

Example of defining a file:

ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT STUDENT-FILE ASSIGN TO "STUDENTS.DAT" ORGANIZATION IS SEQUENTIAL. DATA DIVISION. FILE SECTION. FD STUDENT-FILE. 01 STUDENT-RECORD. 05 STUDENT-NAME PIC X(25). 05 STUDENT-AGE PIC 99.

This defines a simple sequential file named STUDENTS.DAT containing each student’s name and age.


3. Reading and Writing Data to Sequential Files

Here’s a simple example that writes and reads records:

PROCEDURE DIVISION. OPEN OUTPUT STUDENT-FILE. MOVE "PRAVEEN" TO STUDENT-NAME. MOVE 21 TO STUDENT-AGE. WRITE STUDENT-RECORD. CLOSE STUDENT-FILE. OPEN INPUT STUDENT-FILE. READ STUDENT-FILE AT END DISPLAY "NO MORE RECORDS" NOT AT END DISPLAY STUDENT-NAME STUDENT-AGE END-READ. CLOSE STUDENT-FILE. STOP RUN.
  • OPEN OUTPUT creates the file and writes records.

  • OPEN INPUT reads data from the file.

  • READ…AT END handles the end-of-file condition gracefully.

These operations form the foundation for processing real business data in COBOL systems.


🧠 Key Takeaways

  • COBOL handles data through files, especially sequential files.

  • File operations always follow the sequence: OPEN → READ/WRITE → CLOSE.

  • Proper file definitions in the ENVIRONMENT and DATA DIVISION are mandatory.

  • Sequential files are essential for batch processing and legacy business operations.


💻 Practice Task

  1. Create a COBOL program named StudentFileHandler.cbl.

  2. Define a sequential file named STUDENT.DAT.

  3. Accept multiple student records (name and age) and write them to the file.

  4. Then read and display all the records from the same file.

Example outline:

IDENTIFICATION DIVISION. PROGRAM-ID. STUDENT-FILE-HANDLER. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT STUDENT-FILE ASSIGN TO "STUDENT.DAT" ORGANIZATION IS SEQUENTIAL. DATA DIVISION. FILE SECTION. FD STUDENT-FILE. 01 STUDENT-RECORD. 05 STUDENT-NAME PIC X(25). 05 STUDENT-AGE PIC 99. WORKING-STORAGE SECTION. 01 MORE-RECORDS PIC X VALUE "Y". PROCEDURE DIVISION. OPEN OUTPUT STUDENT-FILE. PERFORM UNTIL MORE-RECORDS NOT = "Y" DISPLAY "ENTER STUDENT NAME: " ACCEPT STUDENT-NAME DISPLAY "ENTER AGE: " ACCEPT STUDENT-AGE WRITE STUDENT-RECORD DISPLAY "ADD ANOTHER RECORD (Y/N)? " ACCEPT MORE-RECORDS END-PERFORM. CLOSE STUDENT-FILE. OPEN INPUT STUDENT-FILE. DISPLAY "STUDENT RECORDS:". READ STUDENT-FILE AT END DISPLAY "NO MORE RECORDS" NOT AT END DISPLAY STUDENT-NAME STUDENT-AGE END-READ. CLOSE STUDENT-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