File Organization in COBOL Language

File Organization in COBOL Language

In COBOL, as in any programming language, managing data is a fundamental task. For many business applications, this means handling da

ta stored in external files. Proper file organization is key to efficient data storage, retrieval, and maintenance. In this blog post, we will explore file organization in COBOL, discuss the different file types, and provide examples to illustrate the concepts.

File Types in COBOL Language

COBOL supports several types of files, each designed for specific purposes. The primary file types in COBOL include:

  1. Sequential Files: These are the most straightforward file type and store data in a linear, sequential manner. Records are stored one after the other, making them easy to read from beginning to end. Sequential files are suitable for applications where data is processed in the order it is stored, such as transaction logs or simple text files.
  2. Indexed Files: Indexed files have an associated index that allows for direct access to specific records. This makes them suitable for applications that require rapid record retrieval and modification, like database management systems.
  3. Relative Files: Relative files store records at predefined relative record numbers. They provide both sequential and direct access, making them suitable for applications that need a combination of these access methods.

File Organization COBOL Language

The organization of a file determines how data is physically stored on storage media. COBOL supports several file organizations, including:

  1. Line Sequential (ORGANIZATION IS LINE SEQUENTIAL): This is used with sequential files. Records are stored end-to-end, with each record followed by a newline character. This organization is suitable for plain text files, where each line corresponds to a record.
   SELECT MyFile
       ASSIGN TO "myfile.txt"
       ORGANIZATION IS LINE SEQUENTIAL.
  1. Indexed (ORGANIZATION IS INDEXED): Indexed files use a key to organize data, allowing for rapid access to specific records. This is ideal for applications like customer databases where quick data retrieval is crucial.
   SELECT Customer-File
       ASSIGN TO "customer.dat"
       ORGANIZATION IS INDEXED.
  1. Relative (ORGANIZATION IS RELATIVE): Relative files use relative record numbers to access data. This is useful in scenarios where both sequential and direct access are required.
   SELECT Inventory-File
       ASSIGN TO "inventory.dat"
       ORGANIZATION IS RELATIVE.

Example: Sequential File Organization

Let’s look at an example of creating and reading from a sequentially organized file in COBOL. Suppose we have a file named “inventory.txt” with the following records:

1001,Widget,50,9.99
1002,Gadget,30,19.99
1003,Doohickey,20,14.99

Our COBOL program can read and process this data as follows:

SELECT Inventory-File
   ASSIGN TO "inventory.txt"
   ORGANIZATION IS LINE SEQUENTIAL.

FD Inventory-Record.
01 Inventory-Data.
   02 Item-ID     PIC 9(4).
   02 Description PIC X(15).
   02 Quantity    PIC 9(5).
   02 Price       PIC 9(5)V99.

...

OPEN INPUT Inventory-File

PERFORM UNTIL EOF
   READ Inventory-File
       AT END
           SET EOF TO TRUE
       NOT AT END
           DISPLAY "Item: " Item-ID
           DISPLAY "Description: " Description
           DISPLAY "Quantity: " Quantity
           DISPLAY "Price: " Price
   END-READ
END-PERFORM

CLOSE Inventory-File.

In this example, we declare the file as sequentially organized (ORGANIZATION IS LINE SEQUENTIAL) and define the structure of the data records using the FD and 01 level entries. We then open the file for input, read each record, and process the data.


Discover more from PiEmbSysTech

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech

Subscribe now to keep reading and get access to the full archive.

Continue reading