COBOL File Handling Basics

In COBOL, file handling is a crucial aspect that allows programs to read from and write to different types of files. Understanding how to work with files efficiently can greatly enhance the functionality of your COBOL applications. In this article, we’ll dive into the basics of file handling in COBOL, focusing primarily on sequential files and indexed files.

Understanding COBOL File Types

1. Sequential Files

Sequential files are the simplest type of files in COBOL. As the name suggests, data is stored sequentially, one record after another. This means that when you read or write data, you do so in a linear fashion.

Characteristics of Sequential Files:

  • Data Arrangement: Records are stored one after the other.
  • Access Method: Data can only be accessed starting from the beginning of the file and moving forward.
  • Use Cases: Ideal for applications that require processing of large datasets in a specific order, such as payroll systems or batch processing applications.

Example: Declaring a Sequential File

Here’s an example of how to declare a sequential file in the Data Division:

       DATA DIVISION.
       FILE SECTION.
       
       FD  Employee-File.
       01  Employee-Record.
           05 Employee-ID       PIC 9(6).
           05 Employee-Name     PIC X(30).
           05 Employee-Salary    PIC 9(7)V99.

In this declaration, we define an Employee-File that consists of records containing an ID, name, and salary.

2. Indexed Files

Indexed files, on the other hand, provide a more complex data structure. They allow for more efficient data retrieval, especially when you need to access records out of order. An indexed file uses an index to point to the various records, making search and retrieval operations much faster.

Characteristics of Indexed Files:

  • Data Arrangement: Records can be stored in any order, and an index is maintained.
  • Access Method: Allows random access to records through the index by key fields.
  • Use Cases: Suitable for applications with search capabilities, such as inventory management systems where quick access to records is essential.

Example: Declaring an Indexed File

Here's an example of how to declare an indexed file in the Data Division:

       DATA DIVISION.
       FILE SECTION.

       FD  Department-File.
       01  Department-Record.
           05 Department-ID      PIC 9(4).
           05 Department-Name    PIC X(20).
           05 Department-Budget   PIC 9(8)V99.

       FD  Department-File-Index IS INDEXED BY Department-Index.

In this declaration, we define a Department-File with an index facilitating random access to department records.

Basic File Operations

Now that we understand the types of files in COBOL, let's explore the basic file operations: opening, reading, writing, and closing files.

Opening Files

Files need to be opened before any data operations can be performed. The OPEN statement is used for this purpose. You can open files in three modes: INPUT, OUTPUT, and I-O.

       OPEN INPUT Employee-File
       OPEN OUTPUT Department-File
  • INPUT: Opens the file for reading.
  • OUTPUT: Opens the file for writing (existing content may be erased).
  • I-O: Opens the file for both reading and writing.

Reading Data

Reading data from a sequential file uses the READ statement, which fetches the next record from the file.

       READ Employee-File INTO Employee-Record
           AT END
               DISPLAY "End of file reached."
           NOT AT END
               DISPLAY "Employee ID: " Employee-ID
        END-READ.

In this example, the program reads an employee record until it hits the end of the file.

For indexed files, the retrieval will often involve searching, utilizing the READ statement based on the index:

       READ Department-File-Index 
           INTO Department-Record 
           KEY Department-ID 
           INVALID KEY 
               DISPLAY "Department not found."

Writing Data

To write data, you use the WRITE statement. For sequential files, it simply adds the record at the end of the file.

       WRITE Employee-Record.

For indexed files, you can write a record and also specify the key:

       WRITE Department-Record 
           AFTER ADVANCING 1 LINE.

This statement writes the data into the indexed file efficiently, ensuring proper placement based on the defined keys.

Closing Files

After all operations, it's essential to close the files to release system resources and ensure that data is correctly flushed to the disk. The CLOSE statement is used for this purpose.

       CLOSE Employee-File
       CLOSE Department-File.

File Handling Error Management

Error management is an important aspect of file handling. COBOL provides mechanisms to handle errors during file operations gracefully. The invalid key condition can be used to handle situations where data retrieval does not match expectations.

Here is an example of handling an error during a read operation:

       READ Department-File 
           INTO Department-Record 
           INVALID KEY 
               DISPLAY "Error: Unable to read department record."
           NOT INVALID KEY 
               DISPLAY "Successfully read department record."

Handling such errors ensures that you are prepared for contextual conditions that may arise during file processing.

Conclusion

Understanding COBOL file handling basics is integral to developing robust programs that can manage and process data effectively. By mastering sequential and indexed files, along with the operations of opening, reading, writing, and closing files, you can enhance the capabilities of your COBOL applications.

By now, you should feel more comfortable working with files in COBOL, whether you are reading serial datasets or need to quickly access specific records using indexed files. As you continue your journey, keep practicing and exploring more advanced file handling techniques, as they're key to building efficient COBOL applications!