TSO-ISPF JCL COBOL VSAM DB2 CICS Tools Articles Job Portal Forum Quiz Interview Q&A

COBOL - File Handling Verbs


File handling verbs are used to perform multiple file operations in PROCEDURE DIVISION. The file handling terms are as follows. Processing a file means doing all file operations such as reading, writing, opening, closing, etc.

Following are the list of file handling verbs, or we can say file processing orations:
  1. OPEN - Initiates the processing of files.

  2. READ - Reads a record from the file.

  3. WRITE - Creates a record in the file.

  4. REWRITE - Updates a record in the file.

  5. DELETE - Delete a record in the file.

  6. START - Positioning within an indexed or relative file.

  7. CLOSE - Releases the connection between the file and your program.

  8. READ NEXT - Read next record in the file.

  9. READ PREV - Read previous record in the file.

OPEN:

Before you start working with files, first you need to open the file. Open is the first file operation need to perform before performing other tasks. If open is successful, then only we can perform other file operations like read, write, close, etc. Only after opening a file, the variables in the file structure are available for processing.

No data is transferred to the record buffer by opening a file; it simply provides access.

The file can be opened in 4 modes:
  1. INPUT - This mode is only used for existing files. To read from the file, we need to use this mode. We cannot perform other operations on the file through this mode. When a file is opened for INPUT, the next record pointer points to the file at the beginning of the file.

  2. OUTPUT - Output mode is used to write the records in files. In a sequential file of the record already exists, then the file will be overwritten. But in the case of indexed file and relative file, it will not happen.

  3. EXTEND - This mode appends the records in a sequential file. In this mode, the record will be inserted at the end. When the file is opened for Extend mode, the next record pointer is positioned at the last record in the file.
    We cannot use extend mode in the case of Random and Dynamic file.

  4. I-O - This mode is the input and output mode. This mode reads and rewrites (update) the records of a file.

Syntax:

OPEN modefile-name.

READ:

Once the file has been opened in INPUT or I-O mode, one record at a time can be read using the READ verb. This verb copies a record occurrence or instance from the file and places it in the record buffer defined using FD, and then we can access it.

Syntax: Following is the syntax for read verb when the file access mode is sequential.

READ FILE-NAME [NEXT/PREVIOUS] RECORD [INTO identifier1]
    [AT END {imperative statement}]
    [NOT AT END {imperative statement}]
[END-READ]

Following are the parameters defined in the above syntax:
  • NEXT RECORD: This is optional and is used for sequential reading of an indexed sequential file.

  • INTO: This clause is also optional.

  • AT END: This condition becomes true when the end of the file reached.

Following is the syntax of read verb when the file access mode is random:

READ FILE-NAME RECORD [INTO identifier1][KEY IS key-1]
    [INVALID KEY {imperative statement}]
    [NOT INVALID KEY {imperative statement}]
    [AT END {imperative statement}]
    [NOT AT END {imperative statement}]
    [END-READ]

Example: Go to example page, click here

WRITE:

This verb is used to write the content to a file. To insert data into a file, we must move the data to the record buffer (declared in the FD entry) and then write the contents of record buffer to the file.

Write statement is used to write records directly from the working storage variables by FROM (an optional clause).

Writing operation into the file can be done in two ways based on the file opening mode:
  1. The access mode will be sequential if the file is opened in OUTPUT mode. From the first record, the records will be written. If, before opening in OUTPUT mode, the file has some data that can be refreshed and start writing from the start.

  2. If the file is opened in EXTEND mode, from the last record, the records will be added to the file.

In short, we can say that if the file is opened in OUTPUT mode, then the write operation overwrites the existing file. If the file is opened in EXTEND mode, and then the write verb adds the record to the existing file.

Syntax:

Following is the syntax to write a record when the file organization is sequential:

WRITE record-buffer [FROM ws-file-structure]
END-WRITE.

And following is the syntax to write 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: Go to example page, click here
REWRITE:

Rewrite verb updates the records, but the file must be opened in I-O mode to do the rewrite operation. We can perform the rewrite operation only after a successful reading operation.

Rewrite verb overwrites the last record read. For this, we need to read the record using the READ verb, then change the contents of the record and then perform the REWRITE operation to update the record.

Syntax:

Following is the syntax to write a record when the file organization is sequential:

REWRITE record-buffer [FROM ws-file-structure]
END-REWRITE.

Following is the syntax to write 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: Go to example page, click here

DELETE:

Delete verb is used to delete the record, which is read in the latest read of the file. We can use the Delete verb only on indexed and relatives files. And the file must be opened in I-O mode.

Specific deletion of records in sequential files is not possible.

In case of sequential access mode, the record last read by the Read statement will be deleted. And you need to define the record key in random access mode to perform the deletion process.

Syntax:

DELETE file-name RECORD
   INVALID KEY DISPLAY 'Invalid Key'
   NOT INVALID KEY DISPLAY 'Record Deleted'
END-DELETE.

Example: Go to example page, click here
START:

We can only perform the start operation on indexed and relative files. The start verb is used to place the file pointer at a specific record. This access mode must be sequential or dynamic. The file must be opened in the I-O input mode. The start is not used for retrieving any record, and it only sets the pointer to the next read for reading the record.

Syntax:

Following is the syntax is used 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:

This verb is used to close a file explicitly. When you close a file, then the variables in the file structure will not be available for processing. And the connection between program and file is lost.

Syntax:

CLOSE file-name.

If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal