COBOL - Read Statement Example
Let's see one example which will read the existing file using a sequential organization. This will display all the records written in the file. Input file is EMP-FILE file.
Input: EMP-FILE contains the following content
101 Nikita Kesharwani
102 Deep Ghosh
103 Naina Kukreja
104 Akash Singh |
COBOL Coding:
IDENTIFICATION DIVISION.
PROGRAM-ID. COBLREAD.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT Employee ASSIGN TO EMP-FILE
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD Employee.
01 Employee-FILE.
05 Employee-ID PIC 9(5).
05 NAME PIC A(25).
WORKING-STORAGE SECTION.
01 WS-Employee.
05 WS-Employee-ID PIC 9(5).
05 WS-NAME PIC A(25).
01 WS-EOF PIC A(1).
PROCEDURE DIVISION.
OPEN INPUT Employee.
PERFORM UNTIL WS-EOF='Y'
READ Employee INTO WS-Employee
AT END MOVE 'Y' TO WS-EOF
NOT AT END DISPLAY WS-Employee
END-READ
END-PERFORM.
CLOSE Employee.
STOP RUN. |
When you compile and execute the above program, it produces the following result −
Output:
101 Nikita Kesharwani
102 Deep Ghosh
103 Naina Kukreja
104 Akash Singh |
If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!