MainframeMaster

COBOL Tutorial

COBOL READ Statement

Progress0 of 0 lessons

READ Statement Overview

The READ statement is one of the most fundamental file operations in COBOL. It retrieves the next logical record from a file and makes it available to the program for processing. The behavior of READ varies depending on the file organization and access mode.

Basic READ Operations

COBOL supports several types of READ operations:

  1. Sequential READ - Read records in order from beginning to end
  2. Random READ - Read a specific record by key value
  3. Dynamic READ - Combine sequential and random access
  4. Relative READ - Read records by relative record number

Each type has specific syntax and error handling requirements.

File Organization Compatibility

File OrganizationSequential READRandom READDynamic READ
SEQUENTIAL✓ Yes✗ No✗ No
INDEXED✓ Yes✓ Yes✓ Yes
RELATIVE✓ Yes✓ Yes✓ Yes
LINE SEQUENTIAL✓ Yes✗ No✗ No

Basic READ Syntax

The basic READ statement syntax varies depending on the access mode and file organization. Here are the most common forms:

Sequential READ Syntax

cobol
1
2
3
4
5
READ file-name [NEXT RECORD] [AT END imperative-statement] [NOT AT END imperative-statement] [END-READ]

The NEXT RECORD clause is optional and is the default behavior for sequential reads.

Random READ Syntax

cobol
1
2
3
4
5
READ file-name [RECORD KEY IS data-name] [INVALID KEY imperative-statement] [NOT INVALID KEY imperative-statement] [END-READ]

Random READ requires a RECORD KEY for indexed files or a RELATIVE KEY for relative files.

Basic Examples

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
* Simple sequential read READ CUSTOMER-FILE AT END MOVE "Y" TO EOF-FLAG NOT AT END PERFORM PROCESS-CUSTOMER END-READ. * Random read with key MOVE "12345" TO CUSTOMER-ID READ CUSTOMER-FILE INVALID KEY DISPLAY "Customer not found" NOT INVALID KEY PERFORM PROCESS-CUSTOMER END-READ. * Sequential read with explicit NEXT RECORD READ TRANSACTION-FILE NEXT RECORD AT END PERFORM END-OF-FILE-PROCESSING NOT AT END PERFORM PROCESS-TRANSACTION END-READ.

These examples show the basic patterns for sequential and random READ operations.

Sequential File READ Operations

Sequential files are read from beginning to end, one record at a time. This is the most common and straightforward type of file processing.

Sequential READ with Error Handling

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
* Complete sequential read example IDENTIFICATION DIVISION. PROGRAM-ID. SEQREAD. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.DAT" ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS CUSTOMER-STATUS. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE LABEL RECORDS ARE STANDARD RECORD CONTAINS 80 CHARACTERS. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(5). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-ADDRESS PIC X(35). 05 CUSTOMER-BALANCE PIC 9(7)V99. WORKING-STORAGE SECTION. 01 CUSTOMER-STATUS PIC XX. 01 EOF-FLAG PIC X VALUE "N". 88 END-OF-FILE VALUE "Y". 01 RECORD-COUNT PIC 9(5) VALUE ZERO. PROCEDURE DIVISION. MAIN-PROCESS. PERFORM INITIALIZATION PERFORM READ-PROCESS UNTIL END-OF-FILE PERFORM FINALIZATION STOP RUN. INITIALIZATION. OPEN INPUT CUSTOMER-FILE IF CUSTOMER-STATUS NOT = "00" DISPLAY "Error opening file: " CUSTOMER-STATUS STOP RUN END-IF. READ-PROCESS. READ CUSTOMER-FILE AT END MOVE "Y" TO EOF-FLAG DISPLAY "End of file reached" NOT AT END IF CUSTOMER-STATUS = "00" ADD 1 TO RECORD-COUNT PERFORM PROCESS-CUSTOMER ELSE DISPLAY "Read error: " CUSTOMER-STATUS END-IF END-READ. PROCESS-CUSTOMER. DISPLAY "Processing customer: " CUSTOMER-ID * Add your processing logic here. FINALIZATION. CLOSE CUSTOMER-FILE DISPLAY "Total records processed: " RECORD-COUNT.

This example shows a complete sequential file processing program with proper error handling and status checking.

Sequential READ Options

ClausePurposeWhen to Use
NEXT RECORDExplicitly specify sequential readFor clarity in dynamic access mode
AT ENDHandle end-of-file conditionAlways for sequential reads
NOT AT ENDProcess successful readsWhen you need explicit success handling
END-READEnd the READ statementFor clarity and scope control

Indexed File READ Operations

Indexed files support both sequential and random access. The READ operation behavior depends on the access mode and whether you're using keys for positioning.

Indexed File READ Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
* Sequential read (ACCESS MODE IS SEQUENTIAL) READ file-name [NEXT RECORD] [AT END imperative-statement] [NOT AT END imperative-statement] [END-READ] * Random read (ACCESS MODE IS RANDOM) READ file-name [RECORD KEY IS data-name] [INVALID KEY imperative-statement] [NOT INVALID KEY imperative-statement] [END-READ] * Dynamic read (ACCESS MODE IS DYNAMIC) READ file-name [NEXT RECORD] [RECORD KEY IS data-name] [AT END imperative-statement] [INVALID KEY imperative-statement] [NOT AT END imperative-statement] [NOT INVALID KEY imperative-statement] [END-READ]

The syntax varies based on the access mode specified in the SELECT statement.

Indexed File READ Examples

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
* Sequential access example IDENTIFICATION DIVISION. PROGRAM-ID. IDXSEQ. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.IDX" ORGANIZATION IS INDEXED ACCESS MODE IS SEQUENTIAL RECORD KEY IS CUSTOMER-ID FILE STATUS IS CUSTOMER-STATUS. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE LABEL RECORDS ARE STANDARD RECORD CONTAINS 80 CHARACTERS. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(5). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-ADDRESS PIC X(35). 05 CUSTOMER-BALANCE PIC 9(7)V99. WORKING-STORAGE SECTION. 01 CUSTOMER-STATUS PIC XX. 01 EOF-FLAG PIC X VALUE "N". 88 END-OF-FILE VALUE "Y". PROCEDURE DIVISION. MAIN-PROCESS. OPEN INPUT CUSTOMER-FILE PERFORM READ-SEQUENTIAL UNTIL END-OF-FILE CLOSE CUSTOMER-FILE STOP RUN. READ-SEQUENTIAL. READ CUSTOMER-FILE AT END MOVE "Y" TO EOF-FLAG NOT AT END IF CUSTOMER-STATUS = "00" DISPLAY "Customer: " CUSTOMER-ID " - " CUSTOMER-NAME ELSE DISPLAY "Read error: " CUSTOMER-STATUS END-IF END-READ.

This example shows sequential access to an indexed file, reading records in key order.

Random Access Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
* Random access example IDENTIFICATION DIVISION. PROGRAM-ID. IDXRAND. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.IDX" ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS CUSTOMER-ID FILE STATUS IS CUSTOMER-STATUS. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE LABEL RECORDS ARE STANDARD RECORD CONTAINS 80 CHARACTERS. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(5). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-ADDRESS PIC X(35). 05 CUSTOMER-BALANCE PIC 9(7)V99. WORKING-STORAGE SECTION. 01 CUSTOMER-STATUS PIC XX. 01 SEARCH-ID PIC 9(5). PROCEDURE DIVISION. MAIN-PROCESS. OPEN INPUT CUSTOMER-FILE PERFORM SEARCH-CUSTOMER CLOSE CUSTOMER-FILE STOP RUN. SEARCH-CUSTOMER. DISPLAY "Enter customer ID to search: " ACCEPT SEARCH-ID MOVE SEARCH-ID TO CUSTOMER-ID READ CUSTOMER-FILE INVALID KEY DISPLAY "Customer " SEARCH-ID " not found" NOT INVALID KEY DISPLAY "Customer found:" DISPLAY " ID: " CUSTOMER-ID DISPLAY " Name: " CUSTOMER-NAME DISPLAY " Address: " CUSTOMER-ADDRESS DISPLAY " Balance: " CUSTOMER-BALANCE END-READ.

This example demonstrates random access to an indexed file using a key value.

Dynamic Access with START

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
* Dynamic access with START example IDENTIFICATION DIVISION. PROGRAM-ID. IDXDYN. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.IDX" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS CUSTOMER-ID FILE STATUS IS CUSTOMER-STATUS. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE LABEL RECORDS ARE STANDARD RECORD CONTAINS 80 CHARACTERS. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(5). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-ADDRESS PIC X(35). 05 CUSTOMER-BALANCE PIC 9(7)V99. WORKING-STORAGE SECTION. 01 CUSTOMER-STATUS PIC XX. 01 START-ID PIC 9(5) VALUE 10000. 01 EOF-FLAG PIC X VALUE "N". 88 END-OF-FILE VALUE "Y". PROCEDURE DIVISION. MAIN-PROCESS. OPEN INPUT CUSTOMER-FILE PERFORM START-AND-READ CLOSE CUSTOMER-FILE STOP RUN. START-AND-READ. * Position file at customer ID 10000 MOVE START-ID TO CUSTOMER-ID START CUSTOMER-FILE KEY IS GREATER THAN CUSTOMER-ID INVALID KEY DISPLAY "No customers found with ID > " START-ID NOT INVALID KEY PERFORM READ-FROM-POSITION END-START. READ-FROM-POSITION. PERFORM UNTIL END-OF-FILE READ CUSTOMER-FILE NEXT RECORD AT END MOVE "Y" TO EOF-FLAG NOT AT END IF CUSTOMER-STATUS = "00" DISPLAY "Customer: " CUSTOMER-ID " - " CUSTOMER-NAME ELSE DISPLAY "Read error: " CUSTOMER-STATUS MOVE "Y" TO EOF-FLAG END-IF END-READ END-PERFORM.

This example shows dynamic access using START to position the file, then reading sequentially from that position.

Relative File READ Operations

Relative files allow access by relative record number. The READ operation can be sequential or random, depending on the access mode.

Relative File READ Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
* Sequential read READ file-name [NEXT RECORD] [AT END imperative-statement] [NOT AT END imperative-statement] [END-READ] * Random read by relative record number READ file-name [RELATIVE KEY IS data-name] [INVALID KEY imperative-statement] [NOT INVALID KEY imperative-statement] [END-READ]

For random access, you must set the RELATIVE KEY before the READ operation.

Relative File READ Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
* Relative file read example IDENTIFICATION DIVISION. PROGRAM-ID. RELREAD. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.REL" ORGANIZATION IS RELATIVE ACCESS MODE IS RANDOM RELATIVE KEY IS RELATIVE-RECORD-NUMBER FILE STATUS IS CUSTOMER-STATUS. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE LABEL RECORDS ARE STANDARD RECORD CONTAINS 80 CHARACTERS. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(5). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-ADDRESS PIC X(35). 05 CUSTOMER-BALANCE PIC 9(7)V99. WORKING-STORAGE SECTION. 01 CUSTOMER-STATUS PIC XX. 01 RELATIVE-RECORD-NUMBER PIC 9(5). 01 SEARCH-RECORD-NUMBER PIC 9(5). PROCEDURE DIVISION. MAIN-PROCESS. OPEN INPUT CUSTOMER-FILE PERFORM SEARCH-BY-RECORD-NUMBER CLOSE CUSTOMER-FILE STOP RUN. SEARCH-BY-RECORD-NUMBER. DISPLAY "Enter record number to read: " ACCEPT SEARCH-RECORD-NUMBER MOVE SEARCH-RECORD-NUMBER TO RELATIVE-RECORD-NUMBER READ CUSTOMER-FILE INVALID KEY DISPLAY "Record " SEARCH-RECORD-NUMBER " not found" NOT INVALID KEY DISPLAY "Record found:" DISPLAY " Record #: " RELATIVE-RECORD-NUMBER DISPLAY " Customer ID: " CUSTOMER-ID DISPLAY " Name: " CUSTOMER-NAME DISPLAY " Address: " CUSTOMER-ADDRESS DISPLAY " Balance: " CUSTOMER-BALANCE END-READ.

This example shows random access to a relative file using record numbers.

Error Handling and File Status

Proper error handling is crucial for robust file operations. COBOL provides file status codes and specific clauses to handle different error conditions.

Common File Status Codes

Status CodeMeaningAction Required
"00"Successful operationContinue processing
"02"Duplicate key (write operations)Handle duplicate key
"04"Record length errorCheck record format
"10"End of fileNormal end condition
"22"Duplicate key (indexed files)Handle duplicate key
"23"Record not foundHandle missing record
"30"Permanent errorCheck file/system
"35"File not foundCheck file existence
"37"File not openOpen file first
"39"File conflictCheck file access
"41"File already openClose file first
"42"File not open for inputOpen file for input
"43"File not open for outputOpen file for output
"44"File not open for I/OOpen file for I/O
"46"No next recordEnd of file reached
"47"File not open for extendOpen file for extend
"48"File not open for readOpen file for read
"49"File not open for writeOpen file for write
"90"File information unavailableCheck file system
"91"File not availableCheck file access
"92"Logical file not foundCheck file name
"93"File access not allowedCheck permissions
"94"File organization conflictCheck file type
"95"File access method conflictCheck access mode
"96"File record format conflictCheck record format
"97"File record length conflictCheck record length
"98"File key conflictCheck key definition
"99"File not availableCheck file system

Error Handling Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
* Comprehensive error handling example IDENTIFICATION DIVISION. PROGRAM-ID. READERROR. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.DAT" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS CUSTOMER-ID FILE STATUS IS CUSTOMER-STATUS. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE LABEL RECORDS ARE STANDARD RECORD CONTAINS 80 CHARACTERS. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(5). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-ADDRESS PIC X(35). 05 CUSTOMER-BALANCE PIC 9(7)V99. WORKING-STORAGE SECTION. 01 CUSTOMER-STATUS PIC XX. 01 EOF-FLAG PIC X VALUE "N". 88 END-OF-FILE VALUE "Y". 01 ERROR-FLAG PIC X VALUE "N". 88 ERROR-OCCURRED VALUE "Y". PROCEDURE DIVISION. MAIN-PROCESS. PERFORM INITIALIZATION IF NOT ERROR-OCCURRED PERFORM READ-PROCESS UNTIL END-OF-FILE OR ERROR-OCCURRED END-IF PERFORM FINALIZATION STOP RUN. INITIALIZATION. OPEN INPUT CUSTOMER-FILE EVALUATE CUSTOMER-STATUS WHEN "00" DISPLAY "File opened successfully" WHEN "35" DISPLAY "ERROR: File not found" MOVE "Y" TO ERROR-FLAG WHEN "37" DISPLAY "ERROR: File not open" MOVE "Y" TO ERROR-FLAG WHEN "39" DISPLAY "ERROR: File access conflict" MOVE "Y" TO ERROR-FLAG WHEN OTHER DISPLAY "ERROR: File open failed - Status: " CUSTOMER-STATUS MOVE "Y" TO ERROR-FLAG END-EVALUATE. READ-PROCESS. READ CUSTOMER-FILE AT END MOVE "Y" TO EOF-FLAG DISPLAY "End of file reached" NOT AT END PERFORM CHECK-READ-STATUS END-READ. CHECK-READ-STATUS. EVALUATE CUSTOMER-STATUS WHEN "00" PERFORM PROCESS-CUSTOMER WHEN "10" MOVE "Y" TO EOF-FLAG DISPLAY "End of file reached" WHEN "23" DISPLAY "WARNING: Record not found" WHEN "30" DISPLAY "ERROR: Permanent file error" MOVE "Y" TO ERROR-FLAG WHEN "90" DISPLAY "ERROR: File information unavailable" MOVE "Y" TO ERROR-FLAG WHEN OTHER DISPLAY "ERROR: Read failed - Status: " CUSTOMER-STATUS MOVE "Y" TO ERROR-FLAG END-EVALUATE. PROCESS-CUSTOMER. DISPLAY "Processing customer: " CUSTOMER-ID " - " CUSTOMER-NAME. FINALIZATION. IF NOT ERROR-OCCURRED CLOSE CUSTOMER-FILE IF CUSTOMER-STATUS NOT = "00" DISPLAY "WARNING: File close error - Status: " CUSTOMER-STATUS END-IF END-IF DISPLAY "Program completed".

This example shows comprehensive error handling for file operations, checking status codes at each step.

Practical Examples

These examples demonstrate real-world applications of READ operations for common business scenarios.

Example 1: Customer Lookup System

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
* Customer lookup with multiple access methods IDENTIFICATION DIVISION. PROGRAM-ID. CUSTLOOK. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.IDX" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS CUSTOMER-ID FILE STATUS IS CUSTOMER-STATUS. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE LABEL RECORDS ARE STANDARD RECORD CONTAINS 80 CHARACTERS. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(5). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-ADDRESS PIC X(35). 05 CUSTOMER-BALANCE PIC 9(7)V99. WORKING-STORAGE SECTION. 01 CUSTOMER-STATUS PIC XX. 01 SEARCH-ID PIC 9(5). 01 SEARCH-OPTION PIC X. 01 EOF-FLAG PIC X VALUE "N". 88 END-OF-FILE VALUE "Y". PROCEDURE DIVISION. MAIN-PROCESS. OPEN INPUT CUSTOMER-FILE PERFORM MAIN-MENU CLOSE CUSTOMER-FILE STOP RUN. MAIN-MENU. PERFORM UNTIL SEARCH-OPTION = "X" DISPLAY "Customer Lookup System" DISPLAY "1. Search by Customer ID" DISPLAY "2. List all customers" DISPLAY "3. List customers with balance > 1000" DISPLAY "X. Exit" DISPLAY "Enter option: " ACCEPT SEARCH-OPTION EVALUATE SEARCH-OPTION WHEN "1" PERFORM SEARCH-BY-ID WHEN "2" PERFORM LIST-ALL-CUSTOMERS WHEN "3" PERFORM LIST-HIGH-BALANCE WHEN "X" DISPLAY "Goodbye" WHEN OTHER DISPLAY "Invalid option" END-EVALUATE END-PERFORM. SEARCH-BY-ID. DISPLAY "Enter customer ID: " ACCEPT SEARCH-ID MOVE SEARCH-ID TO CUSTOMER-ID READ CUSTOMER-FILE INVALID KEY DISPLAY "Customer " SEARCH-ID " not found" NOT INVALID KEY PERFORM DISPLAY-CUSTOMER END-READ. LIST-ALL-CUSTOMERS. MOVE "N" TO EOF-FLAG PERFORM READ-SEQUENTIAL UNTIL END-OF-FILE. READ-SEQUENTIAL. READ CUSTOMER-FILE NEXT RECORD AT END MOVE "Y" TO EOF-FLAG NOT AT END IF CUSTOMER-STATUS = "00" PERFORM DISPLAY-CUSTOMER END-IF END-READ. LIST-HIGH-BALANCE. MOVE "N" TO EOF-FLAG PERFORM READ-SEQUENTIAL-HIGH UNTIL END-OF-FILE. READ-SEQUENTIAL-HIGH. READ CUSTOMER-FILE NEXT RECORD AT END MOVE "Y" TO EOF-FLAG NOT AT END IF CUSTOMER-STATUS = "00" AND CUSTOMER-BALANCE > 1000 PERFORM DISPLAY-CUSTOMER END-IF END-READ. DISPLAY-CUSTOMER. DISPLAY "Customer ID: " CUSTOMER-ID DISPLAY "Name: " CUSTOMER-NAME DISPLAY "Address: " CUSTOMER-ADDRESS DISPLAY "Balance: " CUSTOMER-BALANCE DISPLAY "---".

This example shows a customer lookup system with multiple access methods and search options.

Example 2: Transaction Processing

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
* Transaction processing with master file updates IDENTIFICATION DIVISION. PROGRAM-ID. TRANPROC. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT TRANSACTION-FILE ASSIGN TO "TRANS.DAT" ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS TRANS-STATUS. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.IDX" ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS CUSTOMER-ID FILE STATUS IS CUSTOMER-STATUS. DATA DIVISION. FILE SECTION. FD TRANSACTION-FILE LABEL RECORDS ARE STANDARD RECORD CONTAINS 50 CHARACTERS. 01 TRANSACTION-RECORD. 05 TRANS-CUSTOMER-ID PIC 9(5). 05 TRANS-TYPE PIC X. 88 DEBIT VALUE "D". 88 CREDIT VALUE "C". 05 TRANS-AMOUNT PIC 9(7)V99. 05 TRANS-DATE PIC 9(8). 05 TRANS-DESCRIPTION PIC X(30). FD CUSTOMER-FILE LABEL RECORDS ARE STANDARD RECORD CONTAINS 80 CHARACTERS. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(5). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-ADDRESS PIC X(35). 05 CUSTOMER-BALANCE PIC 9(7)V99. WORKING-STORAGE SECTION. 01 FILE-STATUSES. 05 TRANS-STATUS PIC XX. 05 CUSTOMER-STATUS PIC XX. 01 EOF-FLAG PIC X VALUE "N". 88 END-OF-FILE VALUE "Y". 01 PROCESSED-COUNT PIC 9(5) VALUE ZERO. 01 ERROR-COUNT PIC 9(5) VALUE ZERO. PROCEDURE DIVISION. MAIN-PROCESS. PERFORM INITIALIZATION PERFORM PROCESS-TRANSACTIONS UNTIL END-OF-FILE PERFORM FINALIZATION STOP RUN. INITIALIZATION. OPEN INPUT TRANSACTION-FILE OPEN I-O CUSTOMER-FILE IF TRANS-STATUS NOT = "00" OR CUSTOMER-STATUS NOT = "00" DISPLAY "Error opening files" STOP RUN END-IF. PROCESS-TRANSACTIONS. READ TRANSACTION-FILE AT END MOVE "Y" TO EOF-FLAG NOT AT END IF TRANS-STATUS = "00" PERFORM PROCESS-TRANSACTION ELSE ADD 1 TO ERROR-COUNT DISPLAY "Transaction read error: " TRANS-STATUS END-IF END-READ. PROCESS-TRANSACTION. MOVE TRANS-CUSTOMER-ID TO CUSTOMER-ID READ CUSTOMER-FILE INVALID KEY ADD 1 TO ERROR-COUNT DISPLAY "Customer " TRANS-CUSTOMER-ID " not found" NOT INVALID KEY PERFORM UPDATE-CUSTOMER-BALANCE END-READ. UPDATE-CUSTOMER-BALANCE. IF DEBIT SUBTRACT TRANS-AMOUNT FROM CUSTOMER-BALANCE ELSE ADD TRANS-AMOUNT TO CUSTOMER-BALANCE END-IF REWRITE CUSTOMER-RECORD INVALID KEY ADD 1 TO ERROR-COUNT DISPLAY "Error updating customer " CUSTOMER-ID NOT INVALID KEY ADD 1 TO PROCESSED-COUNT DISPLAY "Processed transaction for customer " CUSTOMER-ID END-REWRITE. FINALIZATION. CLOSE TRANSACTION-FILE CLOSE CUSTOMER-FILE DISPLAY "Processing complete" DISPLAY "Transactions processed: " PROCESSED-COUNT DISPLAY "Errors encountered: " ERROR-COUNT.

This example demonstrates transaction processing with master file updates, showing both sequential and random READ operations.

Best Practices and Tips

Following these best practices ensures efficient and reliable READ operations in COBOL programs.

General Best Practices

  • Always check file status codes after READ operations
  • Use AT END clause for sequential reads
  • Use INVALID KEY clause for random reads
  • Initialize key fields before random READ operations
  • Handle all possible error conditions appropriately
  • Close files properly when processing is complete
  • Use meaningful variable names for clarity

Performance Considerations

  • Use sequential access for processing entire files
  • Use random access for specific record lookups
  • Use dynamic access when you need both patterns
  • Position files with START before sequential reads
  • Minimize file opens/closes in loops
  • Use appropriate buffer sizes for optimal I/O performance

Error Handling Guidelines

  • Always include error handling for file operations
  • Log errors appropriately for debugging
  • Provide meaningful error messages to users
  • Handle end-of-file conditions gracefully
  • Check file status after OPEN operations
  • Validate input data before file operations
  • Use structured error handling with EVALUATE statements

Common Pitfalls to Avoid

  • Forgetting to check file status after READ operations
  • Not handling end-of-file conditions in sequential reads
  • Using wrong access mode for the intended operation
  • Not initializing key fields before random reads
  • Ignoring INVALID KEY conditions in random access
  • Not closing files after processing
  • Using sequential access for random lookups
  • Not positioning files before dynamic sequential reads

Test Your Knowledge

1. What is the primary purpose of the READ statement in COBOL?

  • To write data to a file
  • To retrieve the next record from a file
  • To delete records from a file
  • To create a new file

2. Which clause is used to handle end-of-file conditions in a READ statement?

  • ON OVERFLOW
  • AT END
  • ON ERROR
  • WHEN END

3. What is the purpose of the INVALID KEY clause in READ statements?

  • To handle end-of-file conditions
  • To handle record not found or duplicate key conditions
  • To handle file open errors
  • To handle syntax errors

4. Which file organization requires a START statement before random READ operations?

  • Sequential files
  • Indexed files
  • Relative files
  • Line sequential files

5. What does the NEXT RECORD clause specify in a READ statement?

  • Read the previous record
  • Read the next record in sequence
  • Read a specific record by key
  • Read the last record in the file

Frequently Asked Questions