File processing in COBOL involves reading, writing, and manipulating data files using various file organizations, access methods, and record handling techniques. Understanding file processing concepts is fundamental to COBOL programming as most business applications require efficient data file management. Proper file processing ensures data integrity, performance optimization, and reliable data operations.
File processing in COBOL encompasses all methods of handling data files including file definition, record processing, data validation, and file operations. Different file organizations and access methods provide various performance characteristics and capabilities. Understanding these concepts is essential for choosing the right file processing approach for specific business requirements.
Sequential files store records in the order they are written, providing simple and efficient processing for applications that process all records in order. This organization is ideal for batch processing and report generation.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546DATA DIVISION. FILE SECTION. FD INPUT-FILE. 01 INPUT-RECORD. 05 CUSTOMER-ID PIC 9(5). 05 CUSTOMER-NAME PIC X(20). 05 CUSTOMER-BALANCE PIC 9(8)V99. FD OUTPUT-FILE. 01 OUTPUT-RECORD. 05 REPORT-LINE PIC X(80). WORKING-STORAGE SECTION. 01 FILE-CONTROLS. 05 INPUT-STATUS PIC XX VALUE '00'. 05 OUTPUT-STATUS PIC XX VALUE '00'. 88 FILE-OK VALUE '00'. 88 FILE-EOF VALUE '10'. PROCEDURE DIVISION. PROCESS-SEQUENTIAL-FILE. DISPLAY 'Processing sequential file' OPEN INPUT INPUT-FILE OPEN OUTPUT OUTPUT-FILE PERFORM UNTIL FILE-EOF READ INPUT-FILE IF FILE-OK PERFORM PROCESS-RECORD END-IF END-PERFORM CLOSE INPUT-FILE CLOSE OUTPUT-FILE. PROCESS-RECORD. DISPLAY 'Processing record: ' CUSTOMER-ID ' ' CUSTOMER-NAME STRING CUSTOMER-ID DELIMITED BY SIZE ' ' DELIMITED BY SIZE CUSTOMER-NAME DELIMITED BY SIZE ' Balance: ' DELIMITED BY SIZE CUSTOMER-BALANCE DELIMITED BY SIZE INTO REPORT-LINE END-STRING WRITE OUTPUT-RECORD
Sequential file processing reads records in the order they appear in the file. This approach is efficient for batch processing where all records need to be processed in sequence.
Indexed files provide both sequential and random access capabilities using key fields. This organization allows efficient retrieval of specific records and supports complex data relationships.
1234567891011121314151617181920212223242526272829303132333435363738DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(5). 05 CUSTOMER-NAME PIC X(20). 05 CUSTOMER-ADDRESS PIC X(50). 05 CUSTOMER-BALANCE PIC 9(8)V99. WORKING-STORAGE SECTION. 01 FILE-CONTROLS. 05 FILE-STATUS PIC XX VALUE '00'. 88 FILE-OK VALUE '00'. 88 FILE-EOF VALUE '10'. 88 RECORD-FOUND VALUE '00'. 88 RECORD-NOT-FOUND VALUE '23'. PROCEDURE DIVISION. PROCESS-INDEXED-FILE. DISPLAY 'Processing indexed file' OPEN I-O CUSTOMER-FILE *> Random access by customer ID MOVE 12345 TO CUSTOMER-ID READ CUSTOMER-FILE IF RECORD-FOUND DISPLAY 'Found customer: ' CUSTOMER-NAME PERFORM UPDATE-CUSTOMER-BALANCE ELSE DISPLAY 'Customer not found: ' CUSTOMER-ID END-IF CLOSE CUSTOMER-FILE. UPDATE-CUSTOMER-BALANCE. ADD 100.00 TO CUSTOMER-BALANCE REWRITE CUSTOMER-RECORD
Indexed files provide efficient random access to records using key fields. This allows programs to quickly locate and update specific records without reading the entire file.
Record layout definition specifies the structure and format of records within files. Proper record definition ensures data integrity and efficient processing.
123456789101112131415161718192021222324252627282930313233343536373839404142DATA DIVISION. FILE SECTION. FD EMPLOYEE-FILE. 01 EMPLOYEE-RECORD. 05 EMPLOYEE-ID PIC 9(6). 05 EMPLOYEE-NAME. 10 FIRST-NAME PIC X(15). 10 LAST-NAME PIC X(20). 05 EMPLOYEE-ADDRESS. 10 STREET PIC X(30). 10 CITY PIC X(20). 10 STATE PIC X(2). 10 ZIP-CODE PIC X(10). 05 EMPLOYEE-DATA. 10 HIRE-DATE PIC 9(8). 10 SALARY PIC 9(8)V99. 10 DEPARTMENT PIC X(10). WORKING-STORAGE SECTION. 01 RECORD-COUNTERS. 05 TOTAL-RECORDS PIC 9(6) VALUE 0. 05 VALID-RECORDS PIC 9(6) VALUE 0. 05 INVALID-RECORDS PIC 9(6) VALUE 0. PROCEDURE DIVISION. PROCESS-RECORDS. DISPLAY 'Processing employee records' PERFORM UNTIL FILE-EOF READ EMPLOYEE-FILE IF FILE-OK ADD 1 TO TOTAL-RECORDS PERFORM VALIDATE-RECORD IF RECORD-VALID ADD 1 TO VALID-RECORDS PERFORM PROCESS-VALID-RECORD ELSE ADD 1 TO INVALID-RECORDS PERFORM PROCESS-INVALID-RECORD END-IF END-IF END-PERFORM
Record layout definition provides a clear structure for data within files. Hierarchical record structures help organize related data fields logically.
Record validation ensures data integrity by checking record content for correctness, completeness, and business rule compliance before processing.
12345678910111213141516171819202122232425262728293031323334353637PROCEDURE DIVISION. VALIDATE-RECORD. MOVE 'VALID' TO RECORD-STATUS *> Check required fields IF EMPLOYEE-ID = 0 DISPLAY 'ERROR: Employee ID is required' MOVE 'INVALID' TO RECORD-STATUS END-IF IF FIRST-NAME = SPACES DISPLAY 'ERROR: First name is required' MOVE 'INVALID' TO RECORD-STATUS END-IF IF LAST-NAME = SPACES DISPLAY 'ERROR: Last name is required' MOVE 'INVALID' TO RECORD-STATUS END-IF *> Check data ranges IF SALARY < 0 OR SALARY > 999999.99 DISPLAY 'ERROR: Salary out of range: ' SALARY MOVE 'INVALID' TO RECORD-STATUS END-IF *> Check date format IF HIRE-DATE NOT NUMERIC DISPLAY 'ERROR: Invalid hire date: ' HIRE-DATE MOVE 'INVALID' TO RECORD-STATUS END-IF. PROCESS-VALID-RECORD. DISPLAY 'Processing valid record: ' EMPLOYEE-ID ' ' FIRST-NAME ' ' LAST-NAME. PROCESS-INVALID-RECORD. DISPLAY 'Skipping invalid record: ' EMPLOYEE-ID
Record validation ensures data quality by checking required fields, data ranges, and format compliance. Invalid records are identified and handled appropriately.
Sequential access reads records in the order they appear in the file, providing efficient processing for applications that need to process all records.
12345678910111213141516171819202122PROCEDURE DIVISION. SEQUENTIAL-ACCESS. DISPLAY 'Using sequential access' OPEN INPUT INPUT-FILE PERFORM UNTIL FILE-EOF READ INPUT-FILE IF FILE-OK PERFORM PROCESS-CURRENT-RECORD END-IF END-PERFORM CLOSE INPUT-FILE. PROCESS-CURRENT-RECORD. DISPLAY 'Processing record: ' RECORD-NUMBER ADD 1 TO RECORD-NUMBER *> Process record data PERFORM CALCULATE-TOTALS PERFORM UPDATE-STATISTICS
Sequential access is the most efficient method for processing all records in a file. It provides optimal performance for batch processing applications.
Random access allows direct retrieval of specific records using key values, providing efficient access to individual records without reading the entire file.
123456789101112131415161718192021222324252627282930PROCEDURE DIVISION. RANDOM-ACCESS. DISPLAY 'Using random access' OPEN INPUT CUSTOMER-FILE *> Read specific customer by ID MOVE 12345 TO CUSTOMER-ID READ CUSTOMER-FILE IF RECORD-FOUND DISPLAY 'Found customer: ' CUSTOMER-NAME PERFORM PROCESS-CUSTOMER ELSE DISPLAY 'Customer not found: ' CUSTOMER-ID END-IF *> Read another customer MOVE 67890 TO CUSTOMER-ID READ CUSTOMER-FILE IF RECORD-FOUND DISPLAY 'Found customer: ' CUSTOMER-NAME PERFORM PROCESS-CUSTOMER ELSE DISPLAY 'Customer not found: ' CUSTOMER-ID END-IF CLOSE CUSTOMER-FILE. PROCESS-CUSTOMER. DISPLAY 'Processing customer: ' CUSTOMER-ID ' ' CUSTOMER-NAME
Random access provides efficient retrieval of specific records using key values. This method is ideal for applications that need to access individual records quickly.
File status checking monitors file operations for errors and exceptional conditions, providing detailed information about operation results.
123456789101112131415161718192021222324252627282930313233PROCEDURE DIVISION. FILE-OPERATIONS-WITH-STATUS. DISPLAY 'Performing file operations with status checking' OPEN INPUT INPUT-FILE IF NOT FILE-OK DISPLAY 'ERROR: Cannot open input file. Status: ' FILE-STATUS PERFORM FILE-ERROR-HANDLING STOP RUN END-IF PERFORM UNTIL FILE-EOF READ INPUT-FILE EVALUATE FILE-STATUS WHEN '00' PERFORM PROCESS-RECORD WHEN '10' DISPLAY 'End of file reached' WHEN '23' DISPLAY 'ERROR: Record not found' WHEN '24' DISPLAY 'ERROR: File locked' WHEN OTHER DISPLAY 'ERROR: File operation failed. Status: ' FILE-STATUS PERFORM FILE-ERROR-HANDLING END-EVALUATE END-PERFORM CLOSE INPUT-FILE. FILE-ERROR-HANDLING. DISPLAY 'Handling file error with status: ' FILE-STATUS ADD 1 TO ERROR-COUNT
File status checking provides detailed information about file operation results. Different status codes indicate various conditions including success, end-of-file, and error conditions.
Error recovery procedures handle file errors gracefully, providing retry mechanisms, fallback procedures, and clean error handling for various failure conditions.
123456789101112131415161718192021222324252627PROCEDURE DIVISION. ERROR-RECOVERY. DISPLAY 'Implementing error recovery procedures' PERFORM FILE-OPERATION-WITH-RETRY ON EXCEPTION DISPLAY 'File operation failed, attempting recovery' PERFORM RECOVERY-PROCEDURES END-PERFORM. FILE-OPERATION-WITH-RETRY. PERFORM VARYING RETRY-COUNT FROM 1 BY 1 UNTIL RETRY-COUNT > 3 PERFORM ATTEMPT-FILE-OPERATION IF FILE-OK EXIT PERFORM ELSE DISPLAY 'Retry attempt ' RETRY-COUNT ' failed' PERFORM WAIT-BEFORE-RETRY END-IF END-PERFORM. RECOVERY-PROCEDURES. DISPLAY 'Executing recovery procedures' PERFORM CLEANUP-RESOURCES PERFORM LOG-ERROR-DETAILS PERFORM NOTIFY-ADMINISTRATOR
Error recovery procedures provide retry mechanisms and fallback procedures for handling file errors. These procedures help maintain program stability and provide graceful error handling.