Sequential file processing is a fundamental concept in COBOL programming, especially when dealing with large volumes of data stored in files. In this section, we will cover the basics of sequential file processing, including how to read from and write to sequential files.
Key Concepts
- Sequential Files: Files where records are stored one after another in a specific order.
- File Control: The section in a COBOL program where files are defined and their attributes are specified.
- File Handling Statements: COBOL statements used to open, read, write, and close files.
File Control Section
The FILE-CONTROL
paragraph in the ENVIRONMENT DIVISION
is where you define the files your program will use. Here is an 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: Associates a file in the program with an external file.
- ASSIGN TO: Specifies the name of the external file.
- ORGANIZATION IS SEQUENTIAL: Indicates that the file is a sequential file.
Data Division
In the DATA DIVISION
, you define the structure of the records in the files.
DATA DIVISION. FILE SECTION. FD INPUT-FILE. 01 INPUT-RECORD. 05 FIELD1 PIC X(10). 05 FIELD2 PIC 9(5). FD OUTPUT-FILE. 01 OUTPUT-RECORD. 05 FIELD1 PIC X(10). 05 FIELD2 PIC 9(5).
Explanation
- FD: File Description entry, which defines the structure of the file.
- 01: Level number indicating the record structure.
- 05: Level number indicating the fields within the record.
- PIC: Picture clause that defines the data type and size of the field.
Procedure Division
The PROCEDURE DIVISION
contains the logic for processing the files.
Opening Files
Reading from a Sequential File
Writing to a Sequential File
MOVE FIELD1 OF INPUT-RECORD TO FIELD1 OF OUTPUT-RECORD. MOVE FIELD2 OF INPUT-RECORD TO FIELD2 OF OUTPUT-RECORD. WRITE OUTPUT-RECORD.
Closing Files
Complete Example
Here is a complete example that reads records from an input file and writes them to an output file.
IDENTIFICATION DIVISION. PROGRAM-ID. SequentialFileProcessing. 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 FIELD1 PIC X(10). 05 FIELD2 PIC 9(5). FD OUTPUT-FILE. 01 OUTPUT-RECORD. 05 FIELD1 PIC X(10). 05 FIELD2 PIC 9(5). WORKING-STORAGE SECTION. 01 END-OF-FILE PIC X VALUE 'NO'. PROCEDURE DIVISION. OPEN INPUT INPUT-FILE OUTPUT OUTPUT-FILE. PERFORM UNTIL END-OF-FILE = 'YES' READ INPUT-FILE INTO INPUT-RECORD AT END MOVE 'YES' TO END-OF-FILE NOT AT END MOVE FIELD1 OF INPUT-RECORD TO FIELD1 OF OUTPUT-RECORD MOVE FIELD2 OF INPUT-RECORD TO FIELD2 OF OUTPUT-RECORD WRITE OUTPUT-RECORD END-READ END-PERFORM. CLOSE INPUT-FILE OUTPUT-FILE. STOP RUN.
Practical Exercise
Exercise
-
Create an input file named
input.dat
with the following content:John 12345 Jane 67890
-
Write a COBOL program to read the records from
input.dat
and write them tooutput.dat
.
Solution
IDENTIFICATION DIVISION. PROGRAM-ID. SequentialFileProcessingExercise. 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 FIELD1 PIC X(10). 05 FIELD2 PIC 9(5). FD OUTPUT-FILE. 01 OUTPUT-RECORD. 05 FIELD1 PIC X(10). 05 FIELD2 PIC 9(5). WORKING-STORAGE SECTION. 01 END-OF-FILE PIC X VALUE 'NO'. PROCEDURE DIVISION. OPEN INPUT INPUT-FILE OUTPUT OUTPUT-FILE. PERFORM UNTIL END-OF-FILE = 'YES' READ INPUT-FILE INTO INPUT-RECORD AT END MOVE 'YES' TO END-OF-FILE NOT AT END MOVE FIELD1 OF INPUT-RECORD TO FIELD1 OF OUTPUT-RECORD MOVE FIELD2 OF INPUT-RECORD TO FIELD2 OF OUTPUT-RECORD WRITE OUTPUT-RECORD END-READ END-PERFORM. CLOSE INPUT-FILE OUTPUT-FILE. STOP RUN.
Common Mistakes and Tips
- Forgetting to Close Files: Always ensure that you close your files after processing to avoid data corruption.
- Incorrect File Paths: Double-check the file paths in the
ASSIGN TO
clause to ensure they are correct. - Handling End-of-File: Properly handle the end-of-file condition to avoid infinite loops.
Conclusion
In this section, you learned how to process sequential files in COBOL. You now know how to define files, read from them, write to them, and handle end-of-file conditions. This knowledge is essential for working with data stored in files, which is a common requirement in many COBOL applications. In the next section, we will explore indexed file processing, which allows for more efficient data retrieval.