Write Record into Variable Length PS File Example


Scenario - Writing a new record into a new variable length PS file.

Code -

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

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

       DATA DIVISION.
       FILE SECTION.
       FD EMPFILE
           RECORD VARYING FROM 47 TO 80 CHARACTERS
                  DEPENDING ON EMPREC-LEN
           RECORDING MODE  IS  V.

       01 EMP-RECORD80.
          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 EMPREC-LEN    PIC 9(02).
          05 WS-FS1        PIC 9(02).

       PROCEDURE DIVISION.
      * Opening the file for writing 
           OPEN OUTPUT EMPFILE.

           INITIALIZE EMP-RECORD80
      * 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
           MOVE 47   TO EMPREC-LEN
           WRITE EMP-RECORD80
           IF WS-FS1 EQUAL ZERO 
              DISPLAY "Record inserted successfully"  
           ELSE
              DISPLAY "Record insertion failed" WS-FS1
           END-IF.
      * Closing file
           CLOSE EMPFILE.
           STOP RUN.

Run JCL -

000001 //MATEPKF  JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID         
000002 //* 
000003 //STEP01  EXEC PGM=SEQVFLWR
000004 //STEPLIB  DD  DSN=MATEPK.COBOL.LOADLIB,DISP=SHR
000005 //INPUT01  DD  DSN=MATEPK.EMPLOYEE.VINPFILE,
//             DISP=(NEW,CATLG,DELETE),
//             SPACE=(TRK,(1,1),RLSE),
//             UNIT=SYSDA,
//             DCB=(DSORG=PS,RECFM=VB,LRECL=84,BLKSIZE=840)
000006 //SYSOUT   DD  SYSOUT=*
000007 //SYSIN    DD  *
000008 E0006
000009 EMPLOYEE6
000010 SE
000011 0000040000
000012 /*

Output -

Record inserted successfully

MATESY.EMPLOYEE.INPFILE1 -

 BROWSE    MATEPK.EMPLOYEE.VINPFILE                   Line 00000000 Col 001 080 
 Command ===>                                                  Scroll ===> CSR  
********************************* Top of Data **********************************
E0006EMPLOYEE6      SE        0000040000 
******************************** Bottom of Data ********************************

Explaining Example -

In the above example:

  • MATESY.EMPLOYEE.VINPFIL1 is a new variable length PS file created during runtime of the program.
  • ORGANIZATION and ACCESS MODE should be sequential.
  • File should declare with RECORD VARYING...DEPENDING clause to read the file record along with length.
  • The new record inserted into the file as a first record of the file.