Lesson 2

Reading From a Sequential File

Reading From a Sequential File

Welcome back! You've already learned how to create and write to a sequential file in COBOL. Now, it's time to take the next step: reading from a sequential file. This lesson is crucial because it allows you to retrieve and utilize stored data, which is a fundamental aspect of building dynamic applications, especially in sectors like finance, where data retrieval is frequent.

What You'll Learn

In this lesson, you will learn how to read records from an existing sequential file in COBOL. Understanding how to read files will help you build applications that can interact with stored data, enabling you to view, process, and analyze the information.

Let's consider this simple file with two customer details:

Plain text
1John Doe 1234567890 2John Smith 1111111111

Here's a glimpse of the code you'll be working with:

1IDENTIFICATION DIVISION. 2PROGRAM-ID. ReadFromFile. 3ENVIRONMENT DIVISION. 4INPUT-OUTPUT SECTION. 5 6FILE-CONTROL. 7 SELECT InputFile ASSIGN TO 'accounts.dat' 8 ORGANIZATION IS LINE SEQUENTIAL. *> Since each record is in a new line 9 10DATA DIVISION. 11FILE SECTION. 12FD InputFile. 1301 InputRecord. 14 05 Customer-Name PIC X(30). 15 05 Account-Number PIC 9(10). 16 17WORKING-STORAGE SECTION. 1801 WS-EOF PIC X VALUE 'N'. 19 20PROCEDURE DIVISION. 21 OPEN INPUT InputFile. 22 PERFORM UNTIL WS-EOF = 'Y' 23 READ InputFile INTO InputRecord 24 AT END 25 MOVE 'Y' TO WS-EOF 26 DISPLAY "End of file reached." 27 NOT AT END 28 DISPLAY "Customer Name: " Customer-Name 29 DISPLAY "Account Number: " Account-Number 30 END-READ 31 END-PERFORM 32 CLOSE InputFile. 33 STOP RUN.

The output of the code will be:

Plain text
1Customer Name: John Doe 2Account Number: 1234567890 3Customer Name: John Smith 4Account Number: 1111111111 5End of file reached.

Let's analyze the code snippet:

  1. First, we use FILE-CONTROL to define the file we want to read from. We specify the file name accounts.dat and set the organization as LINE SEQUENTIAL, since the file contains records separated by new lines. Note, that similarly you can use the LINE SEQUENTIAL organization if you want to write each record on a new line.
  2. In the DATA DIVISION FILE SECTION, we define the file structure using FD InputFile InputRecord. We specify the fields Customer-Name and Account-Number.
  3. We define a working storage variable WS-EOF to check if we have reached the end of the file.
  4. Next, we move to the PROCEDURE DIVISION. We open the file using OPEN INPUT InputFile.
  5. We use a PERFORM UNTIL loop to read the file until we reach the end of the file. We read the file using READ InputFile INTO InputRecord.
  6. If we reach the end of the file (When AT END returns true), we set WS-EOF to 'Y' and display a message. If we haven't reached the end of the file, we display the customer name and account number.

Finally, we close the file using CLOSE InputFile and stop the program using STOP RUN.

Why It's Important

Being able to read from files is essential for any COBOL programmer. Whether you’re working in banking, retail, or any data-driven industry, you need to know how to access and read data from files. By mastering this skill, you will be able to:

  • Retrieve and display stored data
  • Process and analyze information read from files
  • Perform operations like data validation, updates, and reporting

This ability to read data complements your existing skill in writing to a file, thereby making you proficient in handling both ends of the file management process.

Excited to get started? Let's jump into the practice section and get some hands-on experience with reading from a sequential file in COBOL!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.