File Extend Mode Example


Scenario - Writing a record at the end of the existing PS (Sequential) file.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION. 
       PROGRAM-ID. SEQFILEX.
       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). 

       PROCEDURE DIVISION.
      * Opening EMPFILE for extending mode
           OPEN EXTEND EMPFILE.

           INITIALIZE EMPFILE-RECORD
      * Receiving all the information of the record
           ACCEPT EMP-ID.
           ACCEPT EMP-NAME.
           ACCEPT EMP-DESG.
           ACCEPT EMP-SALARY.
      * Writing record into file and validation
           WRITE EMPFILE-RECORD.
           IF WS-FS1 EQUAL ZERO
              DISPLAY "RECORD INSERTED"
           ELSE
              DISPLAY "RECORD INSERTION FAILED"
           END-IF

      * Closing EMPFILE 
           CLOSE EMPFILE.

           STOP RUN.

Run JCL -

//MATESYF JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID
//*
//STEP01  EXEC PGM=SEQFILEX
//STEPLIB  DD  DSN=MATESY.COBOL.LOADLIB,DISP=SHR
//INPUT01  DD  DSN=MATESY.EMPLOYEE.INPFILE1,
//             DISP=(NEW,CATLG,DELETE),
//             SPACE=(TRK,(1,1),RLSE),
//             UNIT=SYSDA,
//             DCB=(DSORG=PS,RECFM=FB,LRECL=80,BLKSIZE=800)
//SYSOUT   DD  SYSOUT=*
//SYSIN    DD  * 
E0006
EMPLOYEE6 
SE 
0000040000
/*

Output -

RECORD INSERTED   

Output file after Write -

 BROWSE    MATEPK.EMPLOYEE.INPFILE                    Line 00000000 Col 001 080 
 Command ===>                                                  Scroll ===> CSR  
********************************* Top of Data **********************************
E0001EMPLOYEE1      MANAGER   0000200000
E0002EMPLOYEE2      TL        0000150000
E0003EMPLOYEE3      SE        0000050000
E0004EMPLOYEE4      SSE       0000040000
E0005EMPLOYEE5      SE        0000040000
E0006EMPLOYEE6      SE        0000040000
******************************** Bottom of Data ********************************

Explaining Example -

In the above example:

  • MATESY.EMPLOYEE.INPFILE is an existing file.
  • The file should open in EXTEND mode to insert a record at the end of the file.