Reading Fixed-Length Relative File Sequentially Example


Scenario - Reading fixed length RRDS file sequentially.

Input file -

Edit             MATEPK.TEST.RRDS                       
Command ===>                                            
            Type RRDS     Slot            RBA           
                                             Col 1      
       ----+----10---+----2----+----3----+----4----+--  
****** ****  Top of data  ****                          
000001 001 MTH STUDENT1   80 70 85 75 65 97             
000002 002 MTH STUDENT2   60 65 72 83 82 77             
000003 003 MTH STUDENT3   80 70 85 75 65 94             
000004 004 MTH STUDENT4   71 76 67 90 87 85             
000005 005 MTH STUDENT5   75 78 87 82 84 79             
000006 006 MTH STUDENT6   45 63 58 66 59 38             
****** ****  End of data  ****

Code -

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

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

       DATA DIVISION.
       FILE SECTION.
      * EMPFILE structure definition
       FD EMPFILE
           RECORD CONTAINS 47  CHARACTERS
           DATA RECORD     IS EMPFILE-RECORD.
      * EMPFILE record structure definition
       01 EMPFILE-RECORD.
          05 EMP-ID        PIC X(05).
          05 EMP-NAME      PIC X(14). 
          05 EMP-DESG      PIC X(05).
          05 EMP-DEPT      PIC X(05).
          05 EMP-LOC       PIC X(05).
          05 EMP-SALARY    PIC 9(10).
          05 FILLER        PIC X(03).

       WORKING-STORAGE SECTION.
       01 WS-VAR. 
          05 WS-RRN        PIC 9(02).
          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=RELFLRDS
//STEPLIB  DD  DSN=MATEPK.COBOL.LOADLIB,DISP=SHR
//INPUT01  DD  DSN=MATEPK.EMPL.RRDS,DISP=SHR
//SYSOUT   DD  SYSOUT=*

Output -

001 MTH STUDENT1   80 70 85 75 65 97
002 MTH STUDENT2   60 65 72 83 82 77
003 MTH STUDENT3   80 70 85 75 65 94
004 MTH STUDENT4   71 76 67 90 87 85
005 MTH STUDENT5   75 78 87 82 84 79
006 MTH STUDENT6   45 63 58 66 59 38

Explaining Example -

In the above example:

  • Fixed length RRDS (MATEPK.TEST.RRDS) file is used as an input file and trying to read all the records sequentially.
  • File organization is RELATIVE and accessing mode should be SEQUENTIAL.
  • File reading starts from the first record and continue till the end of the file.