Relative file processing in COBOL is a method of accessing records in a file based on their relative position. This type of file organization allows for both sequential and random access to records, making it suitable for applications where records need to be accessed in a non-sequential manner.
Key Concepts
-
Relative File Organization:
- Records are stored in a file with a unique relative record number (RRN).
- The RRN is used to access records directly.
-
Access Modes:
- Sequential Access: Records are processed one after another.
- Random Access: Records are accessed directly using their RRN.
-
File Control Entries:
- Define the file and its attributes in the
FILE-CONTROL
paragraph.
- Define the file and its attributes in the
-
File Section Entries:
- Define the structure of the records in the
FILE SECTION
.
- Define the structure of the records in the
Basic Structure of a COBOL Program with Relative File Processing
File Control Entries
In the ENVIRONMENT DIVISION
, the FILE-CONTROL
paragraph is used to define the relative file.
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT REL-FILE ASSIGN TO 'relative.dat' ORGANIZATION IS RELATIVE ACCESS MODE IS RANDOM RELATIVE KEY IS REL-KEY FILE STATUS IS FILE-STATUS.
File Section Entries
In the DATA DIVISION
, the FILE SECTION
is used to define the structure of the records.
DATA DIVISION. FILE SECTION. FD REL-FILE. 01 REL-RECORD. 05 REL-KEY PIC 9(4). 05 REL-DATA PIC X(20).
Working-Storage Section
Define necessary variables in the WORKING-STORAGE SECTION
.
Procedure Division
The PROCEDURE DIVISION
contains the logic for processing the relative file.
PROCEDURE DIVISION. OPEN I-O REL-FILE. IF FILE-STATUS NOT = '00' DISPLAY 'Error opening file' STOP RUN END-IF. MOVE 1 TO REL-KEY. MOVE 'First Record' TO REL-DATA. WRITE REL-RECORD INVALID KEY DISPLAY 'Error writing record' END-WRITE. MOVE 2 TO REL-KEY. MOVE 'Second Record' TO REL-DATA. WRITE REL-RECORD INVALID KEY DISPLAY 'Error writing record' END-WRITE. MOVE 1 TO REL-KEY. READ REL-FILE INTO WS-RECORD INVALID KEY DISPLAY 'Record not found' END-READ. DISPLAY 'Record 1: ' WS-RECORD. CLOSE REL-FILE. STOP RUN.
Practical Example
Example Program
Here is a complete example of a COBOL program that demonstrates relative file processing.
IDENTIFICATION DIVISION. PROGRAM-ID. RelativeFileExample. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT REL-FILE ASSIGN TO 'relative.dat' ORGANIZATION IS RELATIVE ACCESS MODE IS RANDOM RELATIVE KEY IS REL-KEY FILE STATUS IS FILE-STATUS. DATA DIVISION. FILE SECTION. FD REL-FILE. 01 REL-RECORD. 05 REL-KEY PIC 9(4). 05 REL-DATA PIC X(20). WORKING-STORAGE SECTION. 01 REL-KEY PIC 9(4). 01 FILE-STATUS PIC XX. 01 WS-RECORD PIC X(20). PROCEDURE DIVISION. OPEN I-O REL-FILE. IF FILE-STATUS NOT = '00' DISPLAY 'Error opening file' STOP RUN END-IF. MOVE 1 TO REL-KEY. MOVE 'First Record' TO REL-DATA. WRITE REL-RECORD INVALID KEY DISPLAY 'Error writing record' END-WRITE. MOVE 2 TO REL-KEY. MOVE 'Second Record' TO REL-DATA. WRITE REL-RECORD INVALID KEY DISPLAY 'Error writing record' END-WRITE. MOVE 1 TO REL-KEY. READ REL-FILE INTO WS-RECORD INVALID KEY DISPLAY 'Record not found' END-READ. DISPLAY 'Record 1: ' WS-RECORD. CLOSE REL-FILE. STOP RUN.
Explanation
-
File Control Entries:
- The
SELECT
statement assigns the file name and specifies the organization asRELATIVE
. - The
ACCESS MODE
is set toRANDOM
for direct access. - The
RELATIVE KEY
clause specifies the key used for accessing records. - The
FILE STATUS
clause is used to handle file status codes.
- The
-
File Section Entries:
- The
FD
entry defines the file and its record structure. - The
01
level entry defines the record layout, including the relative key and data fields.
- The
-
Working-Storage Section:
- Variables for the relative key, file status, and record data are defined.
-
Procedure Division:
- The file is opened in I-O mode.
- Records are written to the file using the
WRITE
statement. - Records are read from the file using the
READ
statement. - The program displays the contents of the first record and closes the file.
Practical Exercises
Exercise 1: Writing and Reading Records
Task: Write a COBOL program to create a relative file, write three records to it, and then read and display the records.
Solution:
IDENTIFICATION DIVISION. PROGRAM-ID. RelativeFileExercise. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT REL-FILE ASSIGN TO 'relative.dat' ORGANIZATION IS RELATIVE ACCESS MODE IS RANDOM RELATIVE KEY IS REL-KEY FILE STATUS IS FILE-STATUS. DATA DIVISION. FILE SECTION. FD REL-FILE. 01 REL-RECORD. 05 REL-KEY PIC 9(4). 05 REL-DATA PIC X(20). WORKING-STORAGE SECTION. 01 REL-KEY PIC 9(4). 01 FILE-STATUS PIC XX. 01 WS-RECORD PIC X(20). PROCEDURE DIVISION. OPEN I-O REL-FILE. IF FILE-STATUS NOT = '00' DISPLAY 'Error opening file' STOP RUN END-IF. PERFORM WRITE-RECORDS. PERFORM READ-RECORDS. CLOSE REL-FILE. STOP RUN. WRITE-RECORDS. MOVE 1 TO REL-KEY. MOVE 'First Record' TO REL-DATA. WRITE REL-RECORD INVALID KEY DISPLAY 'Error writing record' END-WRITE. MOVE 2 TO REL-KEY. MOVE 'Second Record' TO REL-DATA. WRITE REL-RECORD INVALID KEY DISPLAY 'Error writing record' END-WRITE. MOVE 3 TO REL-KEY. MOVE 'Third Record' TO REL-DATA. WRITE REL-RECORD INVALID KEY DISPLAY 'Error writing record' END-WRITE. . READ-RECORDS. PERFORM VARYING REL-KEY FROM 1 BY 1 UNTIL REL-KEY > 3 READ REL-FILE INTO WS-RECORD INVALID KEY DISPLAY 'Record not found' END-READ DISPLAY 'Record ' REL-KEY ': ' WS-RECORD END-PERFORM. .
Common Mistakes and Tips
- File Status Handling: Always check the
FILE-STATUS
after file operations to handle errors gracefully. - Invalid Key Handling: Use the
INVALID KEY
clause to manage cases where a record cannot be written or read. - Sequential vs. Random Access: Understand the difference between sequential and random access to use the appropriate method based on the application requirements.
Conclusion
Relative file processing in COBOL provides a flexible way to access records based on their relative position. By understanding the key concepts and practicing with examples, you can effectively use relative files in your COBOL programs. This knowledge is essential for applications that require both sequential and random access to records, such as database management systems and transaction processing systems.