File handling verbs are used to perform various operations on files. Following are the file handling verbs −
- Open
- Read
- Write
- Rewrite
- Delete
- Start
- Close
Open Verb
Open is the first file operation that must be performed. If Open is successful, then only further operations are possible on a file. Only after opening a file, the variables in the file structure are available for processing. FILE STATUS variable is updated after each file operation.
Syntax
OPEN "mode" file-name.
Here, file-name is string literal, which you will use to name your file. A file can be opened in the following modes −
Sr.No. | Mode & Description |
---|---|
1 | InputInput mode is used for existing files. In this mode, we can only read the file, no other operations are allowed on the file. |
2 | OutputOutput mode is used to insert records in files. If a sequential file is used and the file is holding some records, then the existing records will be deleted first and then new records will be inserted in the file. It will not happen so in case of an indexed file or a relative file. |
3 | ExtendExtend mode is used to append records in a sequential file. In this mode, records are inserted at the end. If file access mode is Random or Dynamic, then extend mode cannot be used. |
4 | I-OInput-Output mode is used to read and rewrite the records of a file. |
Read Verb
Read verb is used to read the file records. The function of read is to fetch records from a file. At each read verb, only one record can be read into the file structure. To perform a read operation, open the file in INPUT or I-O mode. At each read statement, the file pointer is incremented and hence the successive records are read.
Syntax
Following is the syntax to read the records when the file access mode is sequential −
READ file-name NEXT RECORD INTO ws-file-structure AT END DISPLAY 'End of File' NOT AT END DISPLAY 'Record Details:' ws-file-structure END-READ.
Following are the parameters used −
- NEXT RECORD is optional and is specified when an indexed sequential file is being read sequentially.
- INTO clause is optional. ws-file-structure is defined in the WorkingStorage Section to get the values from the READ statement.
- AT END condition becomes True when the end of file is reached.
Example − The following example reads an existing file using line sequential organization. This program can be compiled and executed using Live Demo option where it will display all the records present in the file.
IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.ENVIRONMENTDIVISION.INPUT-OUTPUTSECTION.FILE-CONTROL.SELECT STUDENT ASSIGNTO'input.txt'ORGANIZATIONISLINESEQUENTIAL.DATADIVISION.FILESECTION.FD STUDENT.01 STUDENT-FILE.05 STUDENT-ID PIC9(5).05 NAME PICA(25).WORKING-STORAGESECTION.01 WS-STUDENT.05 WS-STUDENT-ID PIC9(5).05 WS-NAME PICA(25).01 WS-EOF PICA(1).PROCEDUREDIVISION.OPENINPUT STUDENT.PERFORMUNTIL WS-EOF='Y'READ STUDENT INTO WS-STUDENTATENDMOVE'Y'TO WS-EOF NOTATENDDISPLAY WS-STUDENT END-READEND-PERFORM.CLOSE STUDENT.STOPRUN.</pre>
Suppose the input file data available in the input.txt file contains the following −
20003 Mohtashim M. 20004 Nishant Malik 20005 Amitabh BachhanWhen you compile and execute the above program, it produces the following result −
20003 Mohtashim M. 20004 Nishant Malik 20005 Amitabh BachhanSyntax
Following is the syntax to write a record when the file access mode is random −
READ file-name RECORD INTO ws-file-structure KEY IS rec-key INVALID KEY DISPLAY 'Invalid Key' NOT INVALID KEY DISPLAY 'Record Details: ' ws-file-structure END-READ.Example − The following example reads an existing file using indexed organization. This program can be compiled and executed using JCL on Mainframes where it will display all the records present in the file. On Mainframes server, we do not use text files; instead we use PS files.
Let's assume that the file present on Mainframes have same content as input.txt file in the above example.
IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.ENVIRONMENTDIVISION.INPUT-OUTPUTSECTION.FILE-CONTROL.SELECT STUDENT ASSIGNTO IN1ORGANIZATIONISINDEXEDACCESSISRANDOMRECORDKEYIS STUDENT-ID FILESTATUSIS FS.DATADIVISION.FILESECTION.FD STUDENT.01 STUDENT-FILE.05 STUDENT-ID PIC9(5).05 NAME PICA(25).WORKING-STORAGESECTION.01 WS-STUDENT.05 WS-STUDENT-ID PIC9(5).05 WS-NAME PICA(25).PROCEDUREDIVISION.OPENINPUT STUDENT.MOVE20005TO STUDENT-ID.READ STUDENT RECORDINTO WS-STUDENT-FILE KEYIS STUDENT-ID INVALIDKEYDISPLAY'Invalid Key'NOTINVALIDKEYDISPLAY WS-STUDENT-FILE END-READ.CLOSE STUDENT.STOPRUN.</pre>
JCL to execute the above COBOL program −
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS= A,MSGCLASS = C //STEP1 EXEC PGM = HELLO //IN1 DD DSN = STUDENT-FILE-NAME,DISP=SHRWhen you compile and execute the above program, it produces the following result −
20005 Amitabh BachhanWrite Verb
Write verb is used to insert records in a file. Once the record is written, it is no longer available in the record buffer. Before inserting records into the file, move the values into the record buffer and then perform write verb.
Write statement can be used with FROM option to directly write records from the working storage variables. From is an optional clause. If the access mode is sequential, then to write a record, the file must open in Output mode or Extend mode. If the access mode is random or dynamic, then to write a record, the file must open in Output mode or I-O mode.
Syntax
Following is the syntax to read a record when the file organization is sequential −
WRITE record-buffer [FROM ws-file-structure] END-WRITE.Following is the syntax to read a record when the file organization is indexed or relative −
WRITE record-buffer [FROM ws-file-structure] INVALID KEY DISPLAY 'Invalid Key' NOT INVALID KEY DISPLAY 'Record Inserted' END-WRITE.Example − The following example shows how to insert a new record in a new file when the organization is sequential.
IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.ENVIRONMENTDIVISION.INPUT-OUTPUTSECTION.FILE-CONTROL.SELECT STUDENT ASSIGNTO OUT101 STUDENT-FILE.05 STUDENT-ID PIC9(5).05 NAME PICA(25).05CLASSPICX(3).WORKING-STORAGESECTION.01 WS-STUDENT.05 WS-STUDENT-ID PIC9(5).05 WS-NAME PICA(25).05 WS-CLASS PICX(3).PROCEDUREDIVISION.OPENEXTEND STUDENT.MOVE1000TO STUDENT-ID.MOVE'Tim'TO NAME.MOVE'10'TOCLASS.WRITE STUDENT-FILEORGANIZATIONISSEQUENTIALACCESSISSEQUENTIALFILESTATUSIS FS.DATADIVISION.FILESECTION.FD STUDENT
END-WRITE.CLOSE STUDENT.STOPRUN.</pre>
JCL to execute the above COBOL program −
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS= A,MSGCLASS = C //STEP1 EXEC PGM = HELLO //OUT1 DD DSN = OUTPUT-FILE-NAME,DISP =(NEW,CATALOG,DELETE)When you compile and execute the above program, it will add a new record to the output file.
1000 Tim 10Rewrite Verb
Rewrite verb is used to update the records. File should be opened in I-O mode for rewrite operations. It can be used only after a successful Read operation. Rewrite verb overwrites the last record read.
Syntax
Following is the syntax to read a record when the file organization is sequential −
REWRITE record-buffer [FROM ws-file-structure] END-REWRITE.Following is the syntax to read a record when the file organization is indexed or relative −
REWRITE record-buffer [FROM ws-file-structure] INVALID KEY DISPLAY 'Invalid Key' NOT INVALID KEY DISPLAY 'Record Updated' END-REWRITE.Example − The following example shows how to update an existing record which we have inserted in the previous Write step −
IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.ENVIRONMENTDIVISION.INPUT-OUTPUTSECTION.FILE-CONTROL.SELECT STUDENT ASSIGNTO IN101 STUDENT-FILE.05 STUDENT-ID PIC9(4).05 NAME PICA(12).05CLASSPICX(3).WORKING-STORAGESECTION.01 WS-STUDENT.05 WS-STUDENT-ID PIC9(5).05 WS-NAME PICA(25).05 WS-CLASS PICX(3).PROCEDUREDIVISION.OPENI-O STUDENT.MOVE'1000'TO STUDENT-ID.READ STUDENTORGANIZATIONISINDEXEDACCESSISRANDOMRECORDKEYIS STUDENT-ID FILESTATUSIS FS.DATADIVISION.FILESECTION.FD STUDENT
END-READ.MOVE'Tim Dumais'TO NAME.REWRITE STUDENT-FILE END-REWRITE.CLOSE STUDENT.STOPRUN.KEYIS STUDENT-ID INVALIDKEYDISPLAYKEYISNOT EXISTING
JCL to execute the above COBOL program −
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS= A,MSGCLASS = C //STEP1 EXEC PGM = HELLO //IN1 DD DSN = OUTPUT-FILE-NAME,DISP = SHRWhen you compile and execute the above program, it will update the record −
1000 Tim Dumais 10Delete Verb
Delete verb can be performed only on indexed and relative files. The file must be opened in I-O mode. In sequential file organization, records cannot be deleted. The record last read by the Read statement is deleted in case of sequential access mode. In random access mode, specify the record key and then perform the Delete operation.
Syntax
Following is the syntax to delete a record −
DELETE file-name RECORD INVALID KEY DISPLAY 'Invalid Key' NOT INVALID KEY DISPLAY 'Record Deleted' END-DELETE.Example − to delete an existing record −
IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.ENVIRONMENTDIVISION.INPUT-OUTPUTSECTION.FILE-CONTROL.SELECT STUDENT ASSIGNTO OUT101 STUDENT-FILE.05 STUDENT-ID PIC9(4).05 NAME PICA(12).05CLASSPICX(3).WORKING-STORAGESECTION.01 WS-STUDENT.05 WS-STUDENT-ID PIC9(5).05 WS-NAME PICA(25).05 WS-CLASS PICX(3).PROCEDUREDIVISION.OPENI-O STUDENT.MOVE'1000'TO STUDENT-ID.DELETE STUDENT RECORDINVALIDKEYDISPLAY'Invalid Key'NOTINVALIDKEYDISPLAY'Record Deleted'END-DELETE.CLOSE STUDENT.STOPRUN.ORGANIZATIONISINDEXEDACCESSISRANDOMRECORDKEYIS STUDENT-ID FILESTATUSIS FS.DATADIVISION.FILESECTION.FD STUDENT
JCL to execute the above COBOL program −
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS= A,MSGCLASS = C //STEP1 EXEC PGM = HELLO //OUT1 DD DSN = OUTPUT-FILE-NAME,DISP = SHRWhen you compile and execute the above program, it produces the following result −
Record DeletedStart Verb
Start verb can be performed only on indexed and relative files. It is used to place the file pointer at a specific record. The access mode must be sequential or dynamic. File must be opened in I-O or Input mode.
Syntax
Following is the syntax to place the pointer at a specific record −
START file-name KEY IS [=, >, <, NOT, <= or >=] rec-key INVALID KEY DISPLAY 'Invalid Key' NOT INVALID KEY DISPLAY 'File Pointer Updated' END-START.Close Verb
Close verb is used to close a file. After performing Close operation, the variables in the file structure will not be available for processing. The link between program and file is lost.
Syntax
Following is the syntax to close a file −
CLOSE file-name.