Open In App

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.

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.



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.

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 :



Hierarchy of a File

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.




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.




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: 

The Syntax for the Open Command is as follows :

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

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:




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: 

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.


Article Tags :