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.
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 text1John Doe 1234567890 2John Smith 1111111111
Here's a glimpse of the code you'll be working with:
cobol1IDENTIFICATION 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 text1Customer Name: John Doe 2Account Number: 1234567890 3Customer Name: John Smith 4Account Number: 1111111111 5End of file reached.
Let's analyze the code snippet:
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.DATA DIVISION
FILE SECTION
, we define the file structure using FD InputFile
InputRecord
. We specify the fields Customer-Name
and Account-Number
.WS-EOF
to check if we have reached the end of the file.PROCEDURE DIVISION
. We open the file using OPEN INPUT InputFile
.PERFORM UNTIL
loop to read the file until we reach the end of the file. We read the file using READ InputFile INTO InputRecord
.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
.
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:
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
!