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. 

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.

Similar Reads

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 :...

Sample COBOL Code for Declaration of a File

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

Sample COBOL Code for Processing of a File

...

Contact Us