Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

File Handling Verbs in COBOL

  • Last Updated : 26 Nov, 2021

The collection of records belonging to the same entity is known as File. These records are stored permanently. File handling helps to organize these records in an ascending/descending order. It makes searching, accessing these records more easily and efficient. The records are generally stored on a magnetic tape or a disk.

Advantages of file handling:

  1. It has unlimited storage and thus stores a large volume of data.
  2. It stores the data permanently on the device.
  3. It reduces the re-editing of data.

Disadvantages of file handling:

  1. It provides slow access.
  2. Cannot perform operations efficiently.

File handling verbs in COBOL helps to perform different desired operations on the files. These verbs are:

  1. OPEN
  2. CLOSE
  3. READ
  4. WRITE
  5. REWRITE
  6. START
  7. DELETE

Below is a detailed description of these file handling verbs.

  • OPEN: OPEN verb opens the file to perform further operations on it i.e. it makes the file available to perform any operation. You cannot perform any operation without opening the file, thus this must be the first operation that should be performed on a file. There are 4 modes in which a file can be opened:
    • INPUT mode: It helps to read the records/data from a file.
    • OUTPUT mode: It helps to write the records/data in a file.
    • EXTEND mode: It helps to write the new record/data at the end of the file, i.e. it does not delete the previous records of the file, unlike the OUTPUT mode.
    • I-O mode: It opens the file in INPUT as well as in OUTPUT mode.

Syntax:

OPEN {INPUT/OUTPUT/EXTEND/I-O} file_name_1[,file_name_2,...].

Example:

Cobol




IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
    FILE-CONTROL.
         SELECT FILE1 ASSIGN TO DISK
         ORGANIZATION LINE SEQUENTIAL.
DATA DIVISION.
    FILE SECTION.
       FD FILE1.
           01 STUDENT.
             02 RNO PIC 99.
             02 NAME PIC A(7).
             02 PERC PIC 99.99.
    WORKING-STORAGE SECTION.
        77 CHOICE PIC 9.
        77 EOF PIC 9.
PROCEDURE DIVISION.
    OPEN OUTPUT FILE1.
    DISPLAY "FILE1 OPENED".
    CLOSE FILE1.
    STOP RUN.
  • CLOSE: A file should be closed after performing all the operations. CLOSE verb disables the link between the file and the program. After performing a close operation the file variables will no longer be available for performing the operation.

Syntax: 

CLOSE file_name_1[,file_name_2,....].

Example:

Cobol




IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
    FILE-CONTROL.
         SELECT FILE1 ASSIGN TO DISK
         ORGANIZATION LINE SEQUENTIAL.
DATA DIVISION.
    FILE SECTION.
       FD FILE1.
           01 STUDENT.
             02 RNO PIC 99.
             02 NAME PIC A(7).
             02 PERC PIC 99.99.
    WORKING-STORAGE SECTION.
        77 CHOICE PIC 9.
        77 EOF PIC 9.
PROCEDURE DIVISION.
    OPEN OUTPUT FILE1.
    DISPLAY "FILE1 OPENED".
    CLOSE FILE1.
    DISPLAY "FILE1 CLOSED".
    STOP RUN.
  • READ:  READ verb allows to read the records of a file. At once, only one record can be read into the file structure and after reading a record the file pointer is incremented by one. To read the records the file must be opened in either INPUT mode or in I-O mode. As soon as the file pointer reaches the end of the file the imperative statement written in “AT END” clause is executed.

Syntax:

READ file-name RECORD [INTO identifier-1] AT END imperative statement. 

Example:

Cobol




IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
        FILE-CONTROL.
            SELECT FILE1 ASSIGN TO DISK
             ORGANIZATION LINE SEQUENTIAL.
DATA DIVISION.
    FILE SECTION.
        FD FILE1.
           01 STUDENT.
             02 RNO PIC 99.
             02 NAME PIC A(7).
             02 PERC PIC 99.99.
    WORKING-STORAGE SECTION.
           77 CHOICE PIC 9.
           77 EOF PIC 9.
PROCEDURE DIVISION.
      OPEN INPUT FILE1.
      PERFORM W-PARA UNTIL EOF = 1.
      CLOSE FILE1.
      STOP RUN.
        
      W-PARA.
             READ FILE1 AT END MOVE 1 TO EOF.
           IF EOF = 0
                   DISPLAY "RNO: ",RNO,"NAME: ",NAME,"PERCENTAGE: ",PERC.
  • WRITE: WRITE verb allows writing the record into the file. At once only one record can be written into the file structure and after writing a record the file pointer is incremented by one, thus the records are written one after the other. To write the records into the file, the file must be opened into OUTPUT mode or I-O mode. If we want to write the values of identifier-1 into the file then the “FROM” clause is execut

    /li>

Syntax:

WRITE record-name [ FROM identifier-1].

Example:

Cobol




IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
        FILE-CONTROL.
            SELECT FILE1 ASSIGN TO DISK
             ORGANIZATION LINE SEQUENTIAL.
DATA DIVISION.
    FILE SECTION.
        FD FILE1.
           01 STUDENT.
             02 RNO PIC 99.
             02 NAME PIC A(7).
             02 PERC PIC 99.99.
    WORKING-STORAGE SECTION.
           77 CHOICE PIC 9.
           77 EOF PIC 9.
PROCEDURE DIVISION.
      OPEN OUTPUT FILE1.
      MOVE 1 TO RNO.
      MOVE 'XYZ' TO NAME.
      MOVE 34.56 TO PERC.
      WRITE STUDENT.
      CLOSE FILE1.
      STOP RUN.
  • REWRITE:  REWRITE verb helps to update an existing record, i.e. if the user wants to rewrite or make any changes to the existing record then use the REWRITE verb. To rewrite the record, the file must be opened in I-O mode. If the user wants to write the values of identifier-1 into the file then the “FROM” clause is executed.

Syntax:

REWRITE record-name [FROM identifier-1].

Example:

Cobol




IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
        FILE-CONTROL.
            SELECT FILE1 ASSIGN TO DISK
             ORGANIZATION LINE SEQUENTIAL.
DATA DIVISION.
    FILE SECTION.
        FD FILE1.
           01 STUDENT.
             02 RNO PIC 99.
             02 NAME PIC A(7).
             02 PERC PIC 99.99.
    WORKING-STORAGE SECTION.
           77 CHOICE PIC 9.
           77 EOF PIC 9.
PROCEDURE DIVISION.
      OPEN I-O FILE1.
      READ FILE1 AT END MOVE 1 TO EOF.
          IF EOF = 0
              IF RNO = 5 
                  MOVE 'XYZ' TO NAME
                  REWRITE STUDENT.
      CLOSE FILE1.
      STOP RUN.
  • START:  If the user wants to start reading the record from a particular location then use the START verb. The file must be opened in I-O mode and the access mode of the file must be either SEQUENTIAL or DYNAMIC. If either the comparison is not satisfied by any key or the file is accessed from an undefined position then the “INVALID KEY” clause is executed.

Syntax: 

START file-name [KEY IS {EQUAL TO/ = /GREATER THAN/ > /NOT LESS THAN/NOT < THAN} data-name] [INVALID KEY imperative statement].

Example:

Cobol




IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
        FILE-CONTROL.
            SELECT FILE1 ASSIGN TO DISK
             ORGANIZATION IS INDEXED.
              ACCESS MODE IS DYNAMIC.
              RECORD-KEY IS RNO.
DATA DIVISION.
    FILE SECTION.
        FD FILE1.
           01 STUDENT.
             02 RNO PIC 99.
             02 NAME PIC A(7).
             02 PERC PIC 99.99.
    WORKING-STORAGE SECTION.
           77 CHOICE PIC 9.
           77 EOF PIC 9.
PROCEDURE DIVISION.
      OPEN I-O FILE1.
      START FILE1 KEY IS NOT LESS THAN 5
          INVALID KEY DISPLAY " WRONG KEY".
      CLOSE FILE1.
      STOP RUN.
  • DELETE: To delete any record from the file use the DELETE verb.  The file must be opened in I-O mode. If the access mode of the file is SEQUENTIAL then the INVALID KEY phrase should not be specified and the DELETE verb must be preceded with the READ statement on the file. If the user tries to delete the record which does not exist in the file then “INVALID KEY” clause is executed.

Syntax:

DELETE file-name [ INVALID KEY imperative statement]. 

Example:

Cobol




IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
        FILE-CONTROL.
            SELECT FILE1 ASSIGN TO DISK
             ORGANIZATION IS INDEXED.
              ACCESS MODE IS DYNAMIC.
              RECORD-KEY IS RNO.
DATA DIVISION.
    FILE SECTION.
        FD FILE1.
           01 STUDENT.
             02 RNO PIC 99.
             02 NAME PIC A(7).
             02 PERC PIC 99.99.
    WORKING-STORAGE SECTION.
           77 CHOICE PIC 9.
           77 EOF PIC 9.
PROCEDURE DIVISION.
      OPEN I-O FILE1.
      MOVE 5 TO RNO.
      DELETE FILE1 
          INVALID KEY DISPLAY " WRONG KEY".
      CLOSE FILE1.
      STOP RUN.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!