File READ Example


Scenario - Reading all the records from a PS file.

Input file -

 BROWSE    MATEPK.EMPLOYEE.INPFILE                    Line 00000000 Col 001 080 
 Command ===>                                                  Scroll ===> CSR  
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
********************************* Top of Data **********************************
E0001EMPLOYEE1      MANAGER   0000200000
E0002EMPLOYEE2      TL        0000150000
E0003EMPLOYEE3      SE        0000050000
E0004EMPLOYEE4      SSE       0000040000
E0005EMPLOYEE5      SE        0000045000
******************************** Bottom of Data ********************************

Code -

----+----1----+----2----+----3----+----4----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. FILEREAD. 
       AUTHOR. MTH.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
      * EMPFILE Definition
           SELECT EMPFILE ASSIGN TO INPUT01
           ORGANIZATION IS SEQUENTIAL
           ACCESS MODE  IS SEQUENTIAL
           FILE STATUS  IS WS-FS1.

       DATA DIVISION.
       FILE SECTION.
      * EMPFILE structure definition
       FD EMPFILE
           RECORD CONTAINS 80  CHARACTERS  
           BLOCK  CONTAINS 800 CHARACTERS      
           RECORDING MODE  IS  F
           DATA RECORD     IS EMPFILE-RECORD.  
      * EMPFILE record structure definition    
       01 EMPFILE-RECORD.
          05 EMP-ID        PIC X(05).
          05 EMP-NAME      PIC X(15).
          05 EMP-DESG      PIC X(10).
          05 EMP-SALARY    PIC 9(10).
          05 FILLER        PIC X(40).

       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-FS1        PIC 9(02).
          05 WS-EOF-SW     PIC X(01).
             88 WS-EOF               VALUE 'Y'.
             88 WS-NOT-EOF           VALUE 'N'.

       PROCEDURE DIVISION.
      * Opening EMPFILE for reading
           OPEN INPUT EMPFILE.

      * Loop for reading all records from EMPFILE
           SET  WS-NOT-EOF      TO  TRUE.
           PERFORM UNTIL WS-EOF
                READ EMPFILE
                         AT END SET WS-EOF TO TRUE     
                     NOT AT END DISPLAY EMPFILE-RECORD 
                END-READ
           END-PERFORM.
      * Closing EMPFILE
           CLOSE EMPFILE.
           STOP RUN.

Run JCL -

//MATEPKF JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID
//*                                                 
//STEP01  EXEC PGM=FILEREAD
//STEPLIB  DD  DSN=MATEPK.COBOL.LOADLIB,DISP=SHR
//INPUT01  DD  DSN=MATEPK.EMPLOYEE.INPFILE,DISP=SHR 
//SYSOUT   DD  SYSOUT=*

Output -

E0001EMPLOYEE1      MANAGER   0000200000
E0002EMPLOYEE2      TL        0000150000
E0003EMPLOYEE3      SE        0000050000
E0004EMPLOYEE4      SSE       0000040000
E0005EMPLOYEE5      SE        0000045000

Explaining Example -

In the above example:

  • Sequential file (PS file) is used as an input file and trying to read all the records sequentially.
  • File reading starts at the first record and continue till the end of the file.