File handling is a crucial aspect of COBOL programming, especially given its extensive use in business, finance, and administrative systems for companies and governments. This section will cover the basics of file handling in COBOL, including how to define, open, read, write, and close files.
Key Concepts
- File Control: Specifies the files used in the program.
- File Description: Describes the structure of the files.
- File Operations: Includes opening, reading, writing, and closing files.
File Control
The FILE-CONTROL paragraph in the ENVIRONMENT DIVISION specifies the files that the program will use. Each file is associated with a file name and a device.
Example:
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INPUT-FILE ASSIGN TO 'input.dat'
ORGANIZATION IS SEQUENTIAL.
SELECT OUTPUT-FILE ASSIGN TO 'output.dat'
ORGANIZATION IS SEQUENTIAL.Explanation:
SELECT INPUT-FILE ASSIGN TO 'input.dat': Associates the logical file nameINPUT-FILEwith the physical fileinput.dat.ORGANIZATION IS SEQUENTIAL: Specifies that the file is a sequential file.
File Description
The FILE SECTION in the DATA DIVISION describes the structure of the files. Each file is defined with a FD (File Description) entry.
Example:
DATA DIVISION.
FILE SECTION.
FD INPUT-FILE.
01 INPUT-RECORD.
05 INPUT-ID PIC 9(5).
05 INPUT-NAME PIC X(20).
FD OUTPUT-FILE.
01 OUTPUT-RECORD.
05 OUTPUT-ID PIC 9(5).
05 OUTPUT-NAME PIC X(20).Explanation:
FD INPUT-FILE: Defines the fileINPUT-FILE.01 INPUT-RECORD: Describes the structure of each record in the file.05 INPUT-ID PIC 9(5): Defines a fieldINPUT-IDwith a picture clause9(5)(5-digit numeric).05 INPUT-NAME PIC X(20): Defines a fieldINPUT-NAMEwith a picture clauseX(20)(20-character alphanumeric).
File Operations
Opening Files
Files must be opened before they can be read or written. The OPEN statement is used for this purpose.
Example:
Explanation:
OPEN INPUT INPUT-FILE: Opens theINPUT-FILEfor reading.OPEN OUTPUT OUTPUT-FILE: Opens theOUTPUT-FILEfor writing.
Reading Files
The READ statement is used to read records from a file.
Example:
Explanation:
READ INPUT-FILE INTO INPUT-RECORD: Reads a record fromINPUT-FILEintoINPUT-RECORD.AT END MOVE 'YES' TO END-OF-FILE: Sets theEND-OF-FILEflag to 'YES' if the end of the file is reached.
Writing Files
The WRITE statement is used to write records to a file.
Example:
Explanation:
MOVE INPUT-ID TO OUTPUT-ID: Copies the value ofINPUT-IDtoOUTPUT-ID.MOVE INPUT-NAME TO OUTPUT-NAME: Copies the value ofINPUT-NAMEtoOUTPUT-NAME.WRITE OUTPUT-RECORD: Writes theOUTPUT-RECORDto theOUTPUT-FILE.
Closing Files
Files must be closed after all operations are completed. The CLOSE statement is used for this purpose.
Example:
Explanation:
CLOSE INPUT-FILE: Closes theINPUT-FILE.CLOSE OUTPUT-FILE: Closes theOUTPUT-FILE.
Practical Exercise
Task:
Write a COBOL program that reads records from an input file and writes them to an output file.
Solution:
IDENTIFICATION DIVISION.
PROGRAM-ID. FileHandlingExample.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INPUT-FILE ASSIGN TO 'input.dat'
ORGANIZATION IS SEQUENTIAL.
SELECT OUTPUT-FILE ASSIGN TO 'output.dat'
ORGANIZATION IS SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD INPUT-FILE.
01 INPUT-RECORD.
05 INPUT-ID PIC 9(5).
05 INPUT-NAME PIC X(20).
FD OUTPUT-FILE.
01 OUTPUT-RECORD.
05 OUTPUT-ID PIC 9(5).
05 OUTPUT-NAME PIC X(20).
WORKING-STORAGE SECTION.
01 END-OF-FILE PIC X(3) VALUE 'NO'.
PROCEDURE DIVISION.
OPEN INPUT INPUT-FILE
OPEN OUTPUT OUTPUT-FILE
PERFORM UNTIL END-OF-FILE = 'YES'
READ INPUT-FILE INTO INPUT-RECORD
AT END
MOVE 'YES' TO END-OF-FILE
END-READ
IF END-OF-FILE NOT = 'YES'
MOVE INPUT-ID TO OUTPUT-ID
MOVE INPUT-NAME TO OUTPUT-NAME
WRITE OUTPUT-RECORD
END-IF
END-PERFORM
CLOSE INPUT-FILE
CLOSE OUTPUT-FILE
STOP RUN.Explanation:
- The program reads records from
input.datand writes them tooutput.dat. - The
PERFORM UNTILloop continues until the end of the input file is reached. - Each record is read from the input file, and if not at the end of the file, it is written to the output file.
Common Mistakes and Tips
- Forgetting to Open or Close Files: Always ensure files are opened before any operations and closed after operations are completed.
- Incorrect File Assignments: Double-check file assignments in the
FILE-CONTROLparagraph. - Mismatched Record Descriptions: Ensure that the record descriptions in the
FILE SECTIONmatch the actual file structure.
Conclusion
In this section, you learned the basics of file handling in COBOL, including how to define, open, read, write, and close files. Understanding these concepts is essential for working with data in COBOL programs. In the next section, we will delve into sequential file processing, which builds on the concepts covered here.
