File Access Mode in COBOL Language

File Access Mode in COBOL Language

File access modes in COBOL are fundamental to how data is read from and written to files. They dictate how a program interacts with e

xternal data files, be it for reading, updating, or appending data. In this blog post, we will delve into file access modes in COBOL and provide examples to illustrate their usage.

File Access Modes in COBOL Language

COBOL supports different file access modes to cater to various data processing requirements:

  1. SEQUENTIAL (ACCESS MODE IS SEQUENTIAL): In this access mode, files are processed sequentially from the beginning to the end. Records are read in the order they appear in the file. Sequential access is suitable for tasks like reading log files, parsing text documents, or processing data files where order matters.
   SELECT Sales-File
      ASSIGN TO "sales.dat"
      ACCESS MODE IS SEQUENTIAL.
  1. RANDOM (ACCESS MODE IS RANDOM): Random access allows the program to read or write records in any order using a record key for identification. It is useful for applications that need quick and direct access to specific records, such as database systems.
   SELECT Employee-File
      ASSIGN TO "employee.dat"
      ACCESS MODE IS RANDOM.
  1. DYNAMIC (ACCESS MODE IS DYNAMIC): Dynamic access provides both sequential and random access to a file. Programs can read records sequentially, and, if needed, directly access specific records using keys. It offers versatility for applications requiring a mix of both access methods.
   SELECT Inventory-File
      ASSIGN TO "inventory.dat"
      ACCESS MODE IS DYNAMIC.

Example: Sequential Access Mode in COBOL Language

Let’s take a look at an example of using sequential access mode in COBOL. Suppose we have a file named “sales.dat” with the following sales records:

2023-01-05,John Doe,Product A,500,1500.00
2023-01-08,Jane Smith,Product B,300,1200.00
2023-01-12,Bob Johnson,Product C,150,900.00

We can create a COBOL program to read these records sequentially:

SELECT Sales-File
   ASSIGN TO "sales.dat"
   ACCESS MODE IS SEQUENTIAL.

FD Sales-Record.
01 Sales-Data.
   02 Sale-Date      PIC X(10).
   02 Salesperson    PIC X(20).
   02 Product        PIC X(15).
   02 Quantity       PIC 9(4).
   02 Total-Sales    PIC 9(7)V99.

...

OPEN INPUT Sales-File

PERFORM UNTIL EOF
   READ Sales-File
       AT END
           SET EOF TO TRUE
       NOT AT END
           DISPLAY "Sale Date: " Sale-Date
           DISPLAY "Salesperson: " Salesperson
           DISPLAY "Product: " Product
           DISPLAY "Quantity: " Quantity
           DISPLAY "Total Sales: " Total-Sales
   END-READ
END-PERFORM

CLOSE Sales-File.

In this example, we specify the file as being in sequential access mode using ACCESS MODE IS SEQUENTIAL. We then define the record layout in the FD section, and finally, open the file, read its records sequentially, 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