Sample COBOL Code for Processing of a File
Processing of a file mainly consists of three steps :
- Opening of a File
- Writing / Re-writing/ Deleting/Retrieving/Reading of a File
- 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.
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.
Contact Us