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:
- It has unlimited storage and thus stores a large volume of data.
- It stores the data permanently on the device.
- It reduces the re-editing of data.
Disadvantages of file handling:
- It provides slow access.
- Cannot perform operations efficiently.
File handling verbs in COBOL helps to perform different desired operations on the files. These verbs are:
- OPEN
- CLOSE
- READ
- WRITE
- REWRITE
- START
- 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 executed.
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.
Similar Reads
File Handling in COBOL
File Handling is an important feature in COBOL Language. Only structured data in the form of files are handled. Files consist of Records and Fields, which contain information. FieldA field can be defined as a collection of information that is required to be given a structure. Each field stores data
12 min read
File Handling in Ruby
It is a way of processing a file such as creating a new file, reading content in a file, writing content to a file, appending content to a file, renaming the file and deleting the file. Common modes for File Handling "r" : Read-only mode for a file. "r+" : Read-Write mode for a file. "w" : Write-onl
4 min read
String Handling in COBOL
The string is the data type, used to represent the sequence of characters, which ends with /0. String provides much functionality which makes our programming easy when it comes to groups of chars, words, sentences, or lines. String Handling:  String handling is the process or method to handle the s
7 min read
File Handling in LISP
LISP is an acronym for list processing, it is one the oldest programming language currently being used in the field of artificial intelligence. It performs its computations on symbolic expressions which makes it too reliable for AI. File handling is a method by which we can access files and store da
4 min read
File Handling in Julia
Julia is a high-level, high-performance, and dynamic programming language that supports file Handling i.e., to read and write files. It is much easier for Julia to open, read, and write files as compared to various other languages. It has a similar way to handle the file as compared to python. It pr
6 min read
File Section in COBOL
COBOL is a high-level programming language for business applications or business use. It was designed in 1959 by the Conference on Data Systems Language (CODASYL). It was primarily used in business, finance, and administration system for companies and governments. COBOL is still widely used in appli
6 min read
Perl | File Handling Introduction
In Perl, file handling is the process of creating, reading, writing, updating, and deleting files. Perl provides a variety of built-in functions and modules that make it easy to work with files. Here's an introduction to file handling in Perl: File modes:When opening a file in Perl, you need to spec
7 min read
File Handling in R Programming
In R Programming, handling of files such as reading and writing files can be done by using in-built functions present in R base package. In this article, let us discuss reading and writing of CSV files, creating a file, renaming a file, check the existence of the file, listing all files in the worki
4 min read
Coding Sheet in COBOL
Every language needs an environment or platform to write codes. For example, in Java, we use notepad to write codes then compile them to run. Similarly, COBOL requires a coding sheet to write codes. COBOL is a business-oriented high-level language. It was developed for business, finance, and adminis
7 min read
File Access Modes in COBOL
COBOL an acronym for Common Business Oriented Language is a computer programming language, which was designed for business use. In COBOL, file access modes are used to specify how to access data from various file organizations according to requirements. In COBOL, there are 3 types of file organizati
3 min read