File handling is a crucial aspect of COBOL programming, especially given its extensive use in business, finance, and administrative systems for companies and governments. This section will cover the basics of file handling in COBOL, including how to define, open, read, write, and close files.

Key Concepts

  1. File Control: Specifies the files used in the program.
  2. File Description: Describes the structure of the files.
  3. File Operations: Includes opening, reading, writing, and closing files.

File Control

The FILE-CONTROL paragraph in the ENVIRONMENT DIVISION specifies the files that the program will use. Each file is associated with a file name and a device.

Example:

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT INPUT-FILE ASSIGN TO 'input.dat'
        ORGANIZATION IS SEQUENTIAL.
    SELECT OUTPUT-FILE ASSIGN TO 'output.dat'
        ORGANIZATION IS SEQUENTIAL.

Explanation:

  • SELECT INPUT-FILE ASSIGN TO 'input.dat': Associates the logical file name INPUT-FILE with the physical file input.dat.
  • ORGANIZATION IS SEQUENTIAL: Specifies that the file is a sequential file.

File Description

The FILE SECTION in the DATA DIVISION describes the structure of the files. Each file is defined with a FD (File Description) entry.

Example:

DATA DIVISION.
FILE SECTION.
FD  INPUT-FILE.
01  INPUT-RECORD.
    05  INPUT-ID     PIC 9(5).
    05  INPUT-NAME   PIC X(20).

FD  OUTPUT-FILE.
01  OUTPUT-RECORD.
    05  OUTPUT-ID    PIC 9(5).
    05  OUTPUT-NAME  PIC X(20).

Explanation:

  • FD INPUT-FILE: Defines the file INPUT-FILE.
  • 01 INPUT-RECORD: Describes the structure of each record in the file.
  • 05 INPUT-ID PIC 9(5): Defines a field INPUT-ID with a picture clause 9(5) (5-digit numeric).
  • 05 INPUT-NAME PIC X(20): Defines a field INPUT-NAME with a picture clause X(20) (20-character alphanumeric).

File Operations

Opening Files

Files must be opened before they can be read or written. The OPEN statement is used for this purpose.

Example:

PROCEDURE DIVISION.
    OPEN INPUT INPUT-FILE
    OPEN OUTPUT OUTPUT-FILE

Explanation:

  • OPEN INPUT INPUT-FILE: Opens the INPUT-FILE for reading.
  • OPEN OUTPUT OUTPUT-FILE: Opens the OUTPUT-FILE for writing.

Reading Files

The READ statement is used to read records from a file.

Example:

    READ INPUT-FILE INTO INPUT-RECORD
        AT END
            MOVE 'YES' TO END-OF-FILE
    END-READ

Explanation:

  • READ INPUT-FILE INTO INPUT-RECORD: Reads a record from INPUT-FILE into INPUT-RECORD.
  • AT END MOVE 'YES' TO END-OF-FILE: Sets the END-OF-FILE flag to 'YES' if the end of the file is reached.

Writing Files

The WRITE statement is used to write records to a file.

Example:

    MOVE INPUT-ID TO OUTPUT-ID
    MOVE INPUT-NAME TO OUTPUT-NAME
    WRITE OUTPUT-RECORD

Explanation:

  • MOVE INPUT-ID TO OUTPUT-ID: Copies the value of INPUT-ID to OUTPUT-ID.
  • MOVE INPUT-NAME TO OUTPUT-NAME: Copies the value of INPUT-NAME to OUTPUT-NAME.
  • WRITE OUTPUT-RECORD: Writes the OUTPUT-RECORD to the OUTPUT-FILE.

Closing Files

Files must be closed after all operations are completed. The CLOSE statement is used for this purpose.

Example:

    CLOSE INPUT-FILE
    CLOSE OUTPUT-FILE

Explanation:

  • CLOSE INPUT-FILE: Closes the INPUT-FILE.
  • CLOSE OUTPUT-FILE: Closes the OUTPUT-FILE.

Practical Exercise

Task:

Write a COBOL program that reads records from an input file and writes them to an output file.

Solution:

IDENTIFICATION DIVISION.
PROGRAM-ID. FileHandlingExample.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT INPUT-FILE ASSIGN TO 'input.dat'
        ORGANIZATION IS SEQUENTIAL.
    SELECT OUTPUT-FILE ASSIGN TO 'output.dat'
        ORGANIZATION IS SEQUENTIAL.

DATA DIVISION.
FILE SECTION.
FD  INPUT-FILE.
01  INPUT-RECORD.
    05  INPUT-ID     PIC 9(5).
    05  INPUT-NAME   PIC X(20).

FD  OUTPUT-FILE.
01  OUTPUT-RECORD.
    05  OUTPUT-ID    PIC 9(5).
    05  OUTPUT-NAME  PIC X(20).

WORKING-STORAGE SECTION.
01  END-OF-FILE     PIC X(3) VALUE 'NO'.

PROCEDURE DIVISION.
    OPEN INPUT INPUT-FILE
    OPEN OUTPUT OUTPUT-FILE

    PERFORM UNTIL END-OF-FILE = 'YES'
        READ INPUT-FILE INTO INPUT-RECORD
            AT END
                MOVE 'YES' TO END-OF-FILE
        END-READ

        IF END-OF-FILE NOT = 'YES'
            MOVE INPUT-ID TO OUTPUT-ID
            MOVE INPUT-NAME TO OUTPUT-NAME
            WRITE OUTPUT-RECORD
        END-IF
    END-PERFORM

    CLOSE INPUT-FILE
    CLOSE OUTPUT-FILE

    STOP RUN.

Explanation:

  • The program reads records from input.dat and writes them to output.dat.
  • The PERFORM UNTIL loop continues until the end of the input file is reached.
  • Each record is read from the input file, and if not at the end of the file, it is written to the output file.

Common Mistakes and Tips

  1. Forgetting to Open or Close Files: Always ensure files are opened before any operations and closed after operations are completed.
  2. Incorrect File Assignments: Double-check file assignments in the FILE-CONTROL paragraph.
  3. Mismatched Record Descriptions: Ensure that the record descriptions in the FILE SECTION match the actual file structure.

Conclusion

In this section, you learned the basics of file handling in COBOL, including how to define, open, read, write, and close files. Understanding these concepts is essential for working with data in COBOL programs. In the next section, we will delve into sequential file processing, which builds on the concepts covered here.

© Copyright 2024. All rights reserved