File Handling Verbs in COBOL

Introduction

File handling verbs are used to perform different operations on files.

File handling Verbs

Different file handling verbs are:

1) Open verb

  • Open Verb is the beginning operation of a file. Without opening a file it is not possible to perform any processing operations like read, write or re-write etc.
  • The variables in the file structure are available for processing only after opening a file.
  • In open verb, FILE-STATUS variable is updated after each file operation.
Syntax:
OPEN “mode” file-name

In above syntax, file-name is string literal use as a file name.

File open in following modes:

1. Input – This mode is used for existing files. File is used to read only.
2. Output – File is used only for writing, i.e to insert a record in the file.
3. Extend – To append records in a sequential file extend mode is used, i.e records are inserted at the end. This mode cannot be used when the file access mode is Random or Dynamic.
4. I-O – Input-Output mode provides read and rewrite the record of a file.

2) Read verb

  • Read Verb is used to read the file records. Only one record is read into the file structure at each read verb.
  • The file should be open in INPUT or I-O mode for performing read operation.
  • The file pointer is incremented at each read statement, hence the consecutive records are read.

Syntax

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.


Note: Above syntax is used when the file access mode is Sequential.

In the above syntax, NEXT RECORD and INTO clause is optional. To get the value from the read READ statement 'ws-file-structure' is defined in the working-storage section. End of file is reached when the AT END condition becomes true.

Syntax

Following syntax is used when the file access mode is Random to read the records:

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 : Read an existing file using sequential organization

IDENTIFICATION DIVISION.
PROGRAM-ID. RV.

ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
      FILE-CONTROL.
      SELECT EMPLOYEE ASSIGN TO 'info.txt'
      ORGANIZATION IS LINE SEQUENTIAL.            

DATA DIVISION.
   FILE SECTION.
   FD EMPLOYEE.
   01 EMPLOYEE-FILE.
      05 EMPLOYEE-ID PIC 9(4).
      05 NAME PIC A(30).

   WORKING-STORAGE SECTION.
   01 WS-EMPLOYEE.
      05 WS-EMPLOYEE-ID PIC 9(4).
      05 WS-NAME PIC A(30).
   01 WS-EOF PIC A(1).

PROCEDURE DIVISION.
   OPEN INPUT EMPLOYEE.
      PERFORM UNTIL WS-EOF='X'
         READ EMPLOYEE INTO WS-EMPLOYEE
            AT END MOVE 'X' TO WS-EOF
            NOT AT END DISPLAY WS-EMPLOYEE
         END-READ
      END-PERFORM.
   CLOSE EMPLOYEE.
STOP RUN.


Output:
Here we assume that, input file data available in the info.txt file and it contains following data:

1003 Abcd
1004 Xyza
1005 Uvwx

After compilation and execution of the above example, it generates the following output:

1003 Abcd
1004 Xyza
1005 Uvwx

3. Write Verb

  • Write verb is used to insert the records in a file. This verb is used when a file is opened in OUTPUT or EXTEND mode.
  • Write statement is used with the FROM option to directly write records from the working storage variables. FROM is an optional clause.
  • For sequential access mode, the file should remain open in Output mode or Extend mode to write a record in a file.
  • For random or dynamic access mode, the file should remain open in Output mode or I-O mode to write a record in a file.

Syntax

Following syntax is used when file organization is sequential:

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

Following syntax is used when file organization is indexed or relative:

WRITE record-buffer [FROM ws-file-strucure]
        INVALID KEY DISPLAY 'Invalid key'
        NOT INVALID KEY DISPLAY 'Record Inserted'
END-WRITE.


4. Rewrite Verb

  • REWRITE verb is used for updating the records in a file.
  • The file must be open in I-O mode for rewrite operations.
  • It is used after the successful read operation.

Syntax

Following syntax is used when file organization is sequential:

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

Following syntax is used when 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.


5) Delete verb

  • Delete verb is executed when the file is indexed and relative.
  • To delete the record file, it should remain open in I-O mode. The record cannot be deleted in sequential file organization.
  • In random access mode, first specify the record key, to perform the delete operation.

Syntax

DELETE file-name RECORD
         INVALID KEY DISPLAY 'Invalid key'
         NOT INVALID KEY DISPLAY 'Record deleted'
END-DELETE.


6) Start verb

  • Start verb is performed only on the indexed and relative files.
  • This verb is used to place the file pointer at a specific record.
  • The access mode should be sequential or dynamic in start verb.
  • In start verb, a file should be open in I-O or input mode.

Syntax

START file-name KEY IS [=, >, <, NOT, <= or >=] rec-key
        INVALID KEY DISPLAY 'Invalid Key'
        NOT INVALID KEY DISPLAY 'File Pointer Updated'
END-START.


7) Close verb

  • Close verb is used to close a file.
  • After executing a close operation the variables in the file structure will not be available for processing and lost the link between the program and file.
Syntax:
CLOSE file-name