Open In App

File Handling in COBOL

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Field

A field can be defined as a collection of information that is required to be given a structure. Each field stores data about an element. A single element represents a particular field. For example, each of the elements –  Employee ID, Name, Grade, and Salary represents a particular field.

  • Primary Keys – It is the column whose values help us to identify each row in a table or a database uniquely. Primary Key prevents duplication of records and helps with updating or deleting only specific records. For example , Driving License Number , Phone Number ,etc.
  • Secondary Keys –  This column can be treated as an alternative to the column which has been identified as Primary Key. It can also be used in addition to Primary Key to locate a specific data. The values in this column may or may not be unique. Secondary Key should not be confused with Foreign Key used in a relational database. While the Foreign Key is used to connect one or more databases together , Secondary Key is used as an alternative or in addition to the Primary Key to search for a specific data. For example , if Student ID is the Primary Key , Student Name or Student Phone Number or both Student Name and Student Phone Number can be treated as a Secondary Key so as to search the specific student’s marks in a particular subject or to know the address of a particular student. 
  • Descriptors –  This field is used for description. For example in a Student Performance File , Marks and Grades add meaning to this record.

Record

Record can be defined as a collection of fields. One or more fields together form a Record. The Records may be of fixed or variable length. For example , Employee ID , Name , Grade and Salary together form one record.

  • Physical Record – The collection of information that exists on the external device is called Physical Record or Block. During input or output operations , this information is handled by the system as a unit.
  • Logical Record – Physical Records are not handled by the COBOL program directly. Instead , it processes Logical Records. Logical Records are nothing but sequences of pieces of information that a COBOL program can process. Logical Records can correspond to complete Physical Records or only to a part of the Physical Record. Also it can correspond to a part or all of one or more Physical Records. The COBOL programs can handle only one particular record at any point of time.

Files and Types of Files

Files are logical representation of large volumes of data in the form of database objects that are stored in a structured way permanently in a memory location. The maximum number files that can be used in a COBOL Program are 255 files. There are three types of files in COBOL :

  • Sequential Files
  • Indexed Files
  • Relative Files

Hierarchy of a File

  • Files contain Records that logical divide the File Data
  • Records contain a set of information which are further divided into Fields.
  • Each Field is further subdivided into characters
  • Finally , a set of characters combined together constitutes an information 

The Files , Records and Fields are identified by File Name , Record Name and Field Name respectively. These are user defined names and must be formed between 1-30 alphanumeric characters.

Working with Files

In order to work with Files , we need to first declare the File and only then we can head onto processing of the File.

Declaration of a File

For declaring a File , File Name and Device name is specified. The Data Structure is also specified as a requirement. The Declaration of Files is done in the Environment or Data Division.

Processing of a File

Entering of records , reading of the records , deletion of records , updating of the records , retrieving of records all fall under the Processing of a File. The Processing of File is done under Procedure Division. 

Sample COBOL Code for Declaration of a File

Let us take an Employee Dataset as an example. The dataset is as follows :

 

The above dataset is used in the following Sample Code.

Cobol




IDENTIFICATION DIVISION.
       PROGRAM-ID. EMPLOYEE-DATA.                 //Line Number 2
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT EMPLOYEE                             //Line Number 6
           ASSIGN TO 'C:\Users\Desktop\Employee.dat'   //Line Number 7


In the above code , in Line Number 2 , Employee-data is given as the Program Name. The Program Name is specified under Identification Division.

In Line Number 6 , we are declaring the File Name. The File Name is declared under File Control. Employee is the name of the file , which is a Logical File and it is assigned to Employee.dat , which is the name of the Physical File , as shown in Line Number 7. It is a data file and with the Assign statement the path where the file is stored is given. Employee.dat is assigned to Job Control Language(JCL) and the JCL is mapped to the physical dataset. JCL is used to execute the COBOL Program.

Cobol




IDENTIFICATION DIVISION.
       PROGRAM-ID. EMPLOYEE-DATA.                    
      //Line Number 2
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT EMPLOYEE                           
      //Line Number 6 
           ASSIGN TO 'C:\Users\Desktop\Employee.dat'  
      //Line Number 7
           ORGANISATION IS LINE SEQUENTIAL.           
      //Line Number 8
       DATA DIVISION.
       FILE SECTION.
       FD EMPLOYEE.                                     
      //Line Number 11 
       01  EMPLOYEE-RECORD.                             
      //Line Number 12 
           05 EMPLOYEE-ID PIC 9(3).                     
      //Line Number 13
           05 FILLER PIC X(10).
           05 EMPLOYEE_NAME PIC X(6).
           05 FILLER PIC X(9).
           05 AGE PIC 9(2).
           05 FILLER PIC X(3).
           05 GRADE PIC X(1).
           05 FILLER PIC X(6).
           05 SALARY PIC 9(5).                           
      //Line Number 21


Here , in Line Number 8 , Organisation is given as Line Sequential , which means that the file should be executed line by line sequentially.Once mapping is done between Logical File Name and Physical File Name , File Description should be given as a next step. 

In the above code , in Line Number 11 , FD is written which stands for File Description. Beside FD , the File Name whose description of records is given in the following lines is provided. The File Name should match with the name of the Logical File that has been declared under File Control. 

In Line Number 12 , a Record Name is declared as Employee Record.

Line Numbers 13 – 21 specify the Field Names. Fillers are also provided which are essentially spaces between each Field. The numbers provided in the bracket of each Filler represents data size of the space or the number of spaces between the previous Field and the next Field.

The Syntax for the specification of Field Names are as follows:

<Level Number> <Field Name> 
<PIC Clause> <Data Type> (<Size>)

Level Number specifies the hierarchy of the data. PIC Clause tells the data type or data category of the field and also its size. Size is represented in bytes. For example , if the Size is 3 , then it implies that the data size is 3 bytes. 

Sample COBOL Code for Processing of a File

Processing of a file mainly consists of three steps :

  1.  Opening of a File
  2. Writing / Re-writing/ Deleting/Retrieving/Reading of a File
  3. Closing of a File

A file is opened with the help of OPEN Command. Then the type of Open mode has to be specified. The types of Open Modes are as follows: 

  • Output Mode – This is mainly used for writing of a data. If we want to write a sequence of records in a file , we use the Output mode. 
  • Input Mode – This mode is used for reading data from an input file.
  • Input-Output Mode – This mode is used for both reading of data from an input file and writing of records or data in a file.
  • Extend Mode – This mode is used for appending data in the existing file.

The Syntax for the Open Command is as follows :

  • If the mode is Output Mode , the Syntax is OPEN OUTPUT <FILENAME>
  • If the mode is Input Mode , the Syntax is OPEN INPUT <FILENAME>
  • If the mode is Input-Output Mode , the Syntax is OPEN INPUT-OUTPUT <FILENAME>
  • If the mode is Extend Mode , the Syntax is OPEN EXTEND <FILENAME>

After Opening a File , we can perform the following operations :

  • Write – This is an output operation. Each time only one record can be written. 
  • Re-write – This is used to change records. Before we can re-write a record , reading of the file with the help of READ has to be performed.
  • Delete – This is another output operation which is used to delete records from the file.
  • Read – This is an input operation which is used to retrieve data from the file.

All the above operations can process only one record at a time. If we want to process multiple records , the PERFORM command has to be kept on a loop.

Now let us look at a Sample Code for Processing of the File. This is the continuation of the above code and it will be used to display some of the records after reading from the File. Here the Processing of the File starts from Line Number 22.

Example 1:

Cobol




IDENTIFICATION DIVISION.
       PROGRAM-ID. EMPLOYEE-DATA.                  
      //Line Number 2
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT EMPLOYEE                         
      //Line Number 6
           ASSIGN TO 'C:\Users\Desktop\Employee.dat'   
      //Line Number 7
           ORGANISATION IS LINE SEQUENTIAL.
      //Line Number 8
       DATA DIVISION.
       FILE SECTION.
       FD EMPLOYEE.                                    
      //Line Number 11
       01  EMPLOYEE-RECORD.                            
      //Line Number 12
           05 EMPLOYEE-ID PIC 9(3).                   
      //Line Number 13
           05 FILLER PIC X(10).
           05 EMPLOYEE_NAME PIC X(6).
           05 FILLER PIC X(9).
           05 AGE PIC 9(2).
           05 FILLER PIC X(3).
           05 GRADE PIC X(1).
           05 FILLER PIC X(6).
           05 SALARY PIC 9(5).                         
      //Line Number 21
       WORKING-STORAGE SECTION.                        
      //Line Number 22
       01  WS-EOF PIC X(1) VALUE 'N'.                  
      //Line Number 23
       PROCEDURE DIVISION.
       MAIN-PROCEDURE.                                 
      //Line Number 25
           OPEN INPUT EMPLOYEE                         
      //Line Number 26
            PERFORM READ-PROCEDURE UNTIL WS-EOF = 'Y'  
      //Line Number 27 
            CLOSE EMPLOYEE                             
      //Line Number 28
            STOP RUN.                                  
      //Line Number 29
       READ-PROCEDURE.
           READ EMPLOYEE    
      //Line Number 31
            AT END MOVE 'Y' TO WS-EOF
            NOT AT END PERFORM DISPLAY-PROCEDURE
           END-READ.
       DISPLAY-PROCEDURE.   
      //Line Number 35 
           IF EMPLOYEE-ID NOT = 'EMP'     
      //Line Number 36
               IF EMPLOYEE-ID NOT = ' ' THEN  
      //Line Number 37
                   DISPLAY 'EMPLOYEE-ID IS :'EMPLOYEE-ID  
      //Line Number 38
                   IF EMPLOYEE_NAME NOT = 'EMPLOY' 
      //Line Number 39
                       IF EMPLOYEE_NAME NOT = ' ' THEN  
      //Line Number 40
       DISPLAY 'EMPLOYEE NAME IS :'EMPLOYEE_NAME  
      //Line Number 41
        IF SALARY NOT = 'SALAR'   
      //Line Number 42
        IF SALARY NOT = ' ' THEN   
      //Line Number 43
        DISPLAY 'EMPLOYEE SALARY IS :'SALARY   
      //Line Number 44
           END-IF
           DISPLAY '-------------------------------------'.


Output: 

 

Explanation: 

  • In the above code, in Line Number 22, Working Storage Section is declared. This section describes records that are not part of the data file. 
  • Under Working Storage Section, in Line Number 23, a variable EOF(End Of Line) is declared whose value is updated as each line of the File is read. This value is initialized to N, as the End of Line has not been reached yet.
  • Line Number 25 denotes the Main Procedure under the Procedure Division. The Main Procedure denotes how each line of the File will be read. 
  • In-Line Number 26, the file is opened.
  • The lines of the File will be read until EOF becomes Y, meaning, that the End Of Line has been reached. This is denoted in Line Number 27.
  • When EOF becomes Y, the file will be closed and the Program will stop running as denoted by Line Numbers 28 and 29 respectively.
  • From Line Number 31, the execution of file reading starts, and simultaneously display of Employee ID, Employee Name and Salary happens on the screen for each row in the file. This display keeps on happening until EOF becomes Y, meaning, the End Of the Line of the above-mentioned file has been reached. 
  • From Line Number 35, Display Procedure starts. Here only the values are supposed to be displayed and the line containing the Heading or if the line is a blank line, those lines are to be skipped and only the values of the mentioned Fields – Employee ID, Employee Name, and Salary will be displayed.
  • Line Number 36  is for the line containing the heading name EMPLOYEE-ID. As we have mentioned under File Section, EMPLOYEE-ID PIC 9(3) , hence only 3 characters, ‘EMP’ is considered.
  • Line Number 39 is for the line containing the heading name EMPLOYEE_NAME. As we have mentioned under File Section, EMPLOYEE_NAME PIC X(6) , hence only 6 characters, ‘EMPLOY’ is considered. 
  • Line Number 42 is for the line containing the heading name SALARY. As we have mentioned under File Section, SALARY PIC 9(5), hence only 5 characters, ‘SALAR’ is considered.
  • Line Numbers 37, 40, and 43 signify that if a line is blank, then that line will be skipped.
  • In-Line Numbers 38, 41, and 44, the process of displaying the values in the Field happens.

This was a simple example of how File Handling is done in COBOL. With the help of COBOL , large of volumes of data in the form of files becomes very easy to handle and work with as the language offers various functionalities. The only downside of File Handling in COBOL is that only one record can be processed at a particular point in time. Even if we use loops to process multiple records , for large volumes of data , the whole process of file reading becomes cumbersome and time-consuming.



Last Updated : 19 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads