File WRITE
File WRITE Example
Scenario - Writing a new record into a new PS file.
Code -
----+----1----+----2----+----3----+----4----+----5----+
IDENTIFICATION DIVISION.
PROGRAM-ID. FILEWRIT.
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 CONTAINS 80 CHARACTERS
BLOCK CONTAINS 800 CHARACTERS
DATA RECORD IS EMPFILE-RECORD.
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) VALUE SPACES.
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 the file for writing
OPEN OUTPUT 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 file
CLOSE EMPFILE.
STOP RUN.
Run JCL -
//MATESYF JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID //* //STEP01 EXEC PGM=FILEWRIT //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
MATESY.EMPLOYEE.INPFILE1 -
E0006EMPLOYEE6 SE 0000040000
Explaining Example -
In the above example:
- MATESY.EMPLOYEE.INPFILE1 is a new file created during runtime of the program.
- The new record inserted into the file as a first record of the file.