You are currently viewing COBOL File Handling Tutorial: Learn to Manage Files in COBOL Programming

COBOL File Handling Tutorial: Learn to Manage Files in COBOL Programming

COBOL, one of the oldest programming languages, is still widely used in legacy systems, particularly in the finance and government sectors. File handling is a crucial aspect of COBOL programming, allowing developers to read from and write to external files efficiently. In this tutorial, we’ll explore various aspects of file handling in COBOL, including file organization techniques, file access modes, and practical code examples.

File Organization in COBOL

In COBOL, files are organized into two main categories: sequential files and indexed files.

  1. Sequential Files: These files contain records that are stored and accessed sequentially, one after the other. Records in sequential files are stored in the order they were added.
  2. Indexed Files: Indexed files use an index to locate records quickly. Each record in an indexed file has a key field, and the index helps in efficient retrieval of records based on this key.

File Access Modes

COBOL supports different file access modes, which determine how files are accessed and modified. The main file access modes in COBOL are:

  1. Input Mode (I): In this mode, files are opened for reading only. Data can be read from the file, but no modifications are allowed.
  2. Output Mode (O): Files opened in output mode are used for writing data. Existing data in the file is overwritten, and new data is appended to the end of the file.
  3. Input/Output Mode (I-O): This mode allows both reading from and writing to files. Records can be read, modified, and rewritten to the file.
  4. Extend Mode (EXTEND): Files opened in extend mode allow writing at the end of the file without overwriting existing data. This mode is typically used for appending records to existing files.

COBOL File Handling Example

Let’s illustrate the concepts of file handling in COBOL with a simple example. Suppose we have a file containing employee records with fields for employee ID, name, and salary. We want to read the records from this file, calculate the total salary, and display the result.

IDENTIFICATION DIVISION.
PROGRAM-ID. FILE-HANDLING-EXAMPLE.

DATA DIVISION.
FILE SECTION.
FD EMPLOYEE-FILE.
01 EMPLOYEE-RECORD.
   05 EMP-ID       PIC X(5).
   05 EMP-NAME     PIC X(20).
   05 EMP-SALARY   PIC 9(6)V99.

WORKING-STORAGE SECTION.
01 TOTAL-SALARY   PIC 9(8)V99 VALUE 0.

PROCEDURE DIVISION.
MAIN-LOGIC.
    OPEN INPUT EMPLOYEE-FILE
    PERFORM READ-EMPLOYEES UNTIL EOF
    DISPLAY "Total Salary: " TOTAL-SALARY
    CLOSE EMPLOYEE-FILE
    STOP RUN.

READ-EMPLOYEES.
    READ EMPLOYEE-FILE INTO EMPLOYEE-RECORD
    AT END SET EOF TO TRUE
    NOT AT END
        ADD EMP-SALARY TO TOTAL-SALARY
    END-READ.

In this example, we open the employee file in input mode (OPEN INPUT EMPLOYEE-FILE) and read records until the end of file (EOF). For each record read (READ EMPLOYEE-FILE INTO EMPLOYEE-RECORD), we add the employee’s salary to the TOTAL-SALARY. Finally, we display the total salary and close the file.

Conclusion

File handling is an essential aspect of COBOL programming, allowing developers to interact with external data efficiently. In this tutorial, we covered the basics of file organization, file access modes, and provided a practical example to illustrate file handling concepts in COBOL. Mastering file handling techniques will enable you to build robust and scalable COBOL applications that effectively manipulate data stored in external files.

Leave a Reply