COBOL Tutorial

Progress0 of 0 lessons

COBOL Record Operations

Record operations are fundamental to COBOL file processing. They allow you to interact with files by reading existing records, writing new ones, updating existing records, and deleting records. Understanding how to use READ, WRITE, REWRITE, and DELETE statements correctly is essential for effective file management in COBOL programs.

Each record operation has specific requirements regarding file modes, access methods, and error handling. This tutorial covers all four main record operations in detail, with practical examples and best practices for each.

Overview of Record Operations

COBOL provides four primary record operations for file manipulation:

  • READ - Retrieves records from a file
  • WRITE - Adds new records to a file
  • REWRITE - Updates existing records in a file
  • DELETE - Removes records from a file

Each operation requires the file to be opened in an appropriate mode and handles errors through file status codes or exception conditions. The choice of operation depends on your file organization (sequential, indexed, or relative) and access mode (sequential, random, or dynamic).

The READ Statement

The READ statement retrieves records from a file. It can be used with sequential, random, or dynamic access modes, depending on your file organization and how you want to access the data.

Sequential READ

Sequential READ reads records one after another in order. It's the most common method for processing entire files from beginning to end.

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
IDENTIFICATION DIVISION. PROGRAM-ID. READ-SEQUENTIAL-EXAMPLE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.DAT" ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS FILE-STATUS-CODE. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUST-ID PIC 9(5). 05 CUST-NAME PIC X(30). 05 CUST-BALANCE PIC S9(7)V99. WORKING-STORAGE SECTION. 01 FILE-STATUS-CODE PIC XX. 88 END-OF-FILE VALUE "10". 88 SUCCESS VALUE "00". PROCEDURE DIVISION. MAIN-PROCESS. OPEN INPUT CUSTOMER-FILE IF FILE-STATUS-CODE NOT = "00" DISPLAY "Error opening file: " FILE-STATUS-CODE STOP RUN END-IF PERFORM READ-RECORDS UNTIL END-OF-FILE CLOSE CUSTOMER-FILE STOP RUN. READ-RECORDS. READ CUSTOMER-FILE AT END DISPLAY "End of file reached" MOVE "10" TO FILE-STATUS-CODE NOT AT END DISPLAY "Customer ID: " CUST-ID DISPLAY "Customer Name: " CUST-NAME DISPLAY "Balance: " CUST-BALANCE END-READ.

In this example, the file is opened in INPUT mode for sequential access. The READ statement retrieves records one at a time. The AT END clause handles the end-of-file condition, while NOT AT END processes each successfully read record. The file status code "10" indicates end of file.

Random READ

Random READ allows you to read a specific record by its key value. This is useful for indexed files where you need to look up specific records without reading through the entire file.

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
IDENTIFICATION DIVISION. PROGRAM-ID. READ-RANDOM-EXAMPLE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.IDX" ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS CUST-ID FILE STATUS IS FILE-STATUS-CODE. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUST-ID PIC 9(5). 05 CUST-NAME PIC X(30). 05 CUST-BALANCE PIC S9(7)V99. WORKING-STORAGE SECTION. 01 FILE-STATUS-CODE PIC XX. 88 RECORD-FOUND VALUE "00". 88 RECORD-NOT-FOUND VALUE "23". 01 SEARCH-ID PIC 9(5). PROCEDURE DIVISION. MAIN-PROCESS. OPEN INPUT CUSTOMER-FILE IF FILE-STATUS-CODE NOT = "00" DISPLAY "Error opening file: " FILE-STATUS-CODE STOP RUN END-IF DISPLAY "Enter customer ID to search: " ACCEPT SEARCH-ID MOVE SEARCH-ID TO CUST-ID READ CUSTOMER-FILE INVALID KEY DISPLAY "Customer " SEARCH-ID " not found" NOT INVALID KEY DISPLAY "Customer found:" DISPLAY " Name: " CUST-NAME DISPLAY " Balance: " CUST-BALANCE END-READ CLOSE CUSTOMER-FILE STOP RUN.

In random access mode, you set the key value (CUST-ID) before reading. The READ statement uses this key to locate the specific record. The INVALID KEY clause handles cases where the record doesn't exist (file status "23"), while NOT INVALID KEY processes the found record.

Dynamic READ with READ NEXT

Dynamic access mode allows you to switch between sequential and random access. You can use READ for random access and READ NEXT for sequential access within the same program.

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
IDENTIFICATION DIVISION. PROGRAM-ID. READ-DYNAMIC-EXAMPLE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.IDX" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS CUST-ID FILE STATUS IS FILE-STATUS-CODE. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUST-ID PIC 9(5). 05 CUST-NAME PIC X(30). 05 CUST-BALANCE PIC S9(7)V99. WORKING-STORAGE SECTION. 01 FILE-STATUS-CODE PIC XX. 88 END-OF-FILE VALUE "10". 01 START-ID PIC 9(5) VALUE 10000. PROCEDURE DIVISION. MAIN-PROCESS. OPEN INPUT CUSTOMER-FILE IF FILE-STATUS-CODE NOT = "00" DISPLAY "Error opening file: " FILE-STATUS-CODE STOP RUN END-IF *> Start sequential read from a specific key MOVE START-ID TO CUST-ID READ CUSTOMER-FILE INVALID KEY DISPLAY "Starting key not found, reading from beginning" START CUSTOMER-FILE KEY >= CUST-ID INVALID KEY DISPLAY "Error positioning file" NOT INVALID KEY PERFORM READ-SEQUENTIALLY END-START NOT INVALID KEY DISPLAY "Starting from customer: " CUST-NAME PERFORM READ-SEQUENTIALLY END-READ CLOSE CUSTOMER-FILE STOP RUN. READ-SEQUENTIALLY. PERFORM UNTIL END-OF-FILE READ CUSTOMER-FILE NEXT AT END DISPLAY "End of file reached" NOT AT END DISPLAY "Customer: " CUST-NAME " Balance: " CUST-BALANCE END-READ END-PERFORM.

Dynamic access provides flexibility by allowing both random and sequential operations. You can position the file at a specific key using START, then read sequentially from that point using READ NEXT. This is useful for processing ranges of records or starting sequential processing from a specific point.

The WRITE Statement

The WRITE statement adds new records to a file. The file mode determines how WRITE behaves: OUTPUT mode creates or overwrites a file, EXTEND mode appends to an existing sequential file, and I-O mode allows writing to indexed or relative files.

WRITE to Sequential File (OUTPUT Mode)

When opening a file in OUTPUT mode, WRITE creates new records. If the file exists, it will be overwritten.

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
IDENTIFICATION DIVISION. PROGRAM-ID. WRITE-OUTPUT-EXAMPLE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.DAT" ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS FILE-STATUS-CODE. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUST-ID PIC 9(5). 05 CUST-NAME PIC X(30). 05 CUST-BALANCE PIC S9(7)V99. WORKING-STORAGE SECTION. 01 FILE-STATUS-CODE PIC XX. 01 RECORD-COUNT PIC 9(5) VALUE 0. PROCEDURE DIVISION. MAIN-PROCESS. OPEN OUTPUT CUSTOMER-FILE IF FILE-STATUS-CODE NOT = "00" DISPLAY "Error opening file: " FILE-STATUS-CODE STOP RUN END-IF PERFORM WRITE-RECORDS UNTIL RECORD-COUNT >= 10 CLOSE CUSTOMER-FILE DISPLAY "Wrote " RECORD-COUNT " records" STOP RUN. WRITE-RECORDS. ADD 1 TO RECORD-COUNT MOVE RECORD-COUNT TO CUST-ID MOVE "Customer Name " TO CUST-NAME MOVE ZEROS TO CUST-BALANCE WRITE CUSTOMER-RECORD IF FILE-STATUS-CODE NOT = "00" DISPLAY "Error writing record: " FILE-STATUS-CODE STOP RUN END-IF.

In OUTPUT mode, each WRITE adds a new record to the file. The records are written in the order they are executed. If the file already exists, it will be replaced with the new data.

WRITE to Sequential File (EXTEND Mode)

EXTEND mode allows you to append records to an existing sequential file without losing existing data.

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
IDENTIFICATION DIVISION. PROGRAM-ID. WRITE-EXTEND-EXAMPLE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.DAT" ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS FILE-STATUS-CODE. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUST-ID PIC 9(5). 05 CUST-NAME PIC X(30). 05 CUST-BALANCE PIC S9(7)V99. WORKING-STORAGE SECTION. 01 FILE-STATUS-CODE PIC XX. PROCEDURE DIVISION. MAIN-PROCESS. *> Open in EXTEND mode to append records OPEN EXTEND CUSTOMER-FILE IF FILE-STATUS-CODE NOT = "00" DISPLAY "Error opening file: " FILE-STATUS-CODE STOP RUN END-IF *> Write new records that will be appended MOVE 10001 TO CUST-ID MOVE "New Customer 1" TO CUST-NAME MOVE 1500.50 TO CUST-BALANCE WRITE CUSTOMER-RECORD MOVE 10002 TO CUST-ID MOVE "New Customer 2" TO CUST-NAME MOVE 2300.75 TO CUST-BALANCE WRITE CUSTOMER-RECORD CLOSE CUSTOMER-FILE DISPLAY "Records appended successfully" STOP RUN.

EXTEND mode positions the file pointer at the end of the file, so all WRITE operations append new records after the existing ones. This is useful for adding new data without recreating the entire file.

WRITE to Indexed File (I-O Mode)

For indexed files, you can write new records when the file is opened in I-O mode. The system checks for duplicate keys and handles them according to your file definition.

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
IDENTIFICATION DIVISION. PROGRAM-ID. WRITE-INDEXED-EXAMPLE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.IDX" ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS CUST-ID FILE STATUS IS FILE-STATUS-CODE. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUST-ID PIC 9(5). 05 CUST-NAME PIC X(30). 05 CUST-BALANCE PIC S9(7)V99. WORKING-STORAGE SECTION. 01 FILE-STATUS-CODE PIC XX. 88 SUCCESS VALUE "00". 88 DUPLICATE-KEY VALUE "22". PROCEDURE DIVISION. MAIN-PROCESS. OPEN I-O CUSTOMER-FILE IF FILE-STATUS-CODE NOT = "00" DISPLAY "Error opening file: " FILE-STATUS-CODE STOP RUN END-IF PERFORM ADD-CUSTOMER CLOSE CUSTOMER-FILE STOP RUN. ADD-CUSTOMER. DISPLAY "Enter customer ID: " ACCEPT CUST-ID DISPLAY "Enter customer name: " ACCEPT CUST-NAME DISPLAY "Enter balance: " ACCEPT CUST-BALANCE WRITE CUSTOMER-RECORD INVALID KEY IF DUPLICATE-KEY DISPLAY "Customer ID " CUST-ID " already exists" ELSE DISPLAY "Error writing record: " FILE-STATUS-CODE END-IF NOT INVALID KEY DISPLAY "Customer added successfully" END-WRITE.

When writing to an indexed file, the system checks if a record with the same key already exists. If it does, the INVALID KEY condition is triggered with file status "22" (duplicate key). The NOT INVALID KEY clause handles successful writes.

The REWRITE Statement

The REWRITE statement updates an existing record in a file. Before using REWRITE, you must first READ the record you want to update. REWRITE replaces the last record that was read.

REWRITE with Sequential Access

For sequential files, REWRITE updates the last record that was read. This is useful for updating records as you process a file sequentially.

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
IDENTIFICATION DIVISION. PROGRAM-ID. REWRITE-SEQUENTIAL-EXAMPLE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.DAT" ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS FILE-STATUS-CODE. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUST-ID PIC 9(5). 05 CUST-NAME PIC X(30). 05 CUST-BALANCE PIC S9(7)V99. WORKING-STORAGE SECTION. 01 FILE-STATUS-CODE PIC XX. 88 END-OF-FILE VALUE "10". PROCEDURE DIVISION. MAIN-PROCESS. OPEN I-O CUSTOMER-FILE IF FILE-STATUS-CODE NOT = "00" DISPLAY "Error opening file: " FILE-STATUS-CODE STOP RUN END-IF PERFORM UPDATE-RECORDS UNTIL END-OF-FILE CLOSE CUSTOMER-FILE STOP RUN. UPDATE-RECORDS. READ CUSTOMER-FILE AT END CONTINUE NOT AT END *> Update balance for customers with ID less than 5000 IF CUST-ID < 5000 ADD 100.00 TO CUST-BALANCE REWRITE CUSTOMER-RECORD IF FILE-STATUS-CODE NOT = "00" DISPLAY "Error rewriting record: " FILE-STATUS-CODE ELSE DISPLAY "Updated customer " CUST-ID END-IF END-IF END-READ.

In sequential access, you read records one by one. When you find a record that needs updating, you modify it in memory and use REWRITE to write it back. REWRITE replaces the record at the current file position (the last record read).

REWRITE with Random Access

For indexed files with random access, you read a specific record by key, modify it, then rewrite it.

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
IDENTIFICATION DIVISION. PROGRAM-ID. REWRITE-RANDOM-EXAMPLE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.IDX" ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS CUST-ID FILE STATUS IS FILE-STATUS-CODE. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUST-ID PIC 9(5). 05 CUST-NAME PIC X(30). 05 CUST-BALANCE PIC S9(7)V99. WORKING-STORAGE SECTION. 01 FILE-STATUS-CODE PIC XX. 88 RECORD-FOUND VALUE "00". 88 RECORD-NOT-FOUND VALUE "23". 01 SEARCH-ID PIC 9(5). PROCEDURE DIVISION. MAIN-PROCESS. OPEN I-O CUSTOMER-FILE IF FILE-STATUS-CODE NOT = "00" DISPLAY "Error opening file: " FILE-STATUS-CODE STOP RUN END-IF PERFORM UPDATE-CUSTOMER CLOSE CUSTOMER-FILE STOP RUN. UPDATE-CUSTOMER. DISPLAY "Enter customer ID to update: " ACCEPT SEARCH-ID MOVE SEARCH-ID TO CUST-ID READ CUSTOMER-FILE INVALID KEY DISPLAY "Customer " SEARCH-ID " not found" NOT INVALID KEY DISPLAY "Current information:" DISPLAY " Name: " CUST-NAME DISPLAY " Balance: " CUST-BALANCE DISPLAY "Enter new name (or press ENTER to keep current): " ACCEPT CUST-NAME DISPLAY "Enter new balance: " ACCEPT CUST-BALANCE REWRITE CUSTOMER-RECORD INVALID KEY DISPLAY "Error updating record: " FILE-STATUS-CODE NOT INVALID KEY DISPLAY "Customer updated successfully" END-REWRITE END-READ.

In random access mode, you specify the key of the record you want to update, read it, modify the data, and then rewrite it. This is the most common pattern for updating specific records in indexed files.

The DELETE Statement

The DELETE statement removes records from indexed or relative files. DELETE cannot be used with sequential files. For sequential access, you must read the record first; for random access, you can delete by key.

DELETE with Sequential Access

When using sequential access, you must read the record before you can delete it.

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
IDENTIFICATION DIVISION. PROGRAM-ID. DELETE-SEQUENTIAL-EXAMPLE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.IDX" ORGANIZATION IS INDEXED ACCESS MODE IS SEQUENTIAL RECORD KEY IS CUST-ID FILE STATUS IS FILE-STATUS-CODE. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUST-ID PIC 9(5). 05 CUST-NAME PIC X(30). 05 CUST-BALANCE PIC S9(7)V99. 05 CUST-STATUS PIC X. WORKING-STORAGE SECTION. 01 FILE-STATUS-CODE PIC XX. 88 END-OF-FILE VALUE "10". PROCEDURE DIVISION. MAIN-PROCESS. OPEN I-O CUSTOMER-FILE IF FILE-STATUS-CODE NOT = "00" DISPLAY "Error opening file: " FILE-STATUS-CODE STOP RUN END-IF PERFORM DELETE-INACTIVE-CUSTOMERS UNTIL END-OF-FILE CLOSE CUSTOMER-FILE STOP RUN. DELETE-INACTIVE-CUSTOMERS. READ CUSTOMER-FILE AT END CONTINUE NOT AT END *> Delete customers with status 'I' (Inactive) IF CUST-STATUS = "I" DELETE CUSTOMER-FILE RECORD INVALID KEY DISPLAY "Error deleting customer " CUST-ID NOT INVALID KEY DISPLAY "Deleted inactive customer " CUST-ID END-DELETE END-IF END-READ.

In sequential access, you read each record and check if it should be deleted. If so, you use DELETE to remove it. The DELETE statement removes the last record that was read.

DELETE with Random Access

With random access, you can delete a record directly by its key without reading it first (though reading first is often recommended to verify the record exists).

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
IDENTIFICATION DIVISION. PROGRAM-ID. DELETE-RANDOM-EXAMPLE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.IDX" ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS CUST-ID FILE STATUS IS FILE-STATUS-CODE. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUST-ID PIC 9(5). 05 CUST-NAME PIC X(30). 05 CUST-BALANCE PIC S9(7)V99. WORKING-STORAGE SECTION. 01 FILE-STATUS-CODE PIC XX. 88 RECORD-NOT-FOUND VALUE "23". 01 SEARCH-ID PIC 9(5). 01 CONFIRM-DELETE PIC X. PROCEDURE DIVISION. MAIN-PROCESS. OPEN I-O CUSTOMER-FILE IF FILE-STATUS-CODE NOT = "00" DISPLAY "Error opening file: " FILE-STATUS-CODE STOP RUN END-IF PERFORM DELETE-CUSTOMER CLOSE CUSTOMER-FILE STOP RUN. DELETE-CUSTOMER. DISPLAY "Enter customer ID to delete: " ACCEPT SEARCH-ID *> Read first to display information and confirm MOVE SEARCH-ID TO CUST-ID READ CUSTOMER-FILE INVALID KEY DISPLAY "Customer " SEARCH-ID " not found" NOT INVALID KEY DISPLAY "Customer to delete:" DISPLAY " Name: " CUST-NAME DISPLAY " Balance: " CUST-BALANCE DISPLAY "Are you sure? (Y/N): " ACCEPT CONFIRM-DELETE IF CONFIRM-DELETE = "Y" DELETE CUSTOMER-FILE RECORD INVALID KEY DISPLAY "Error deleting customer: " FILE-STATUS-CODE NOT INVALID KEY DISPLAY "Customer " SEARCH-ID " deleted successfully" END-DELETE ELSE DISPLAY "Deletion cancelled" END-IF END-READ.

In random access mode, you can delete by key. It's good practice to read the record first to display its information and get user confirmation before deleting. This prevents accidental deletions and provides better user feedback.

Complete Example: File Maintenance Program

Here's a complete example that demonstrates all four record operations in a customer maintenance program:

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
IDENTIFICATION DIVISION. PROGRAM-ID. CUSTOMER-MAINTENANCE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.IDX" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS CUST-ID FILE STATUS IS FILE-STATUS-CODE. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUST-ID PIC 9(5). 05 CUST-NAME PIC X(30). 05 CUST-ADDRESS PIC X(50). 05 CUST-BALANCE PIC S9(7)V99. 05 CUST-STATUS PIC X. WORKING-STORAGE SECTION. 01 FILE-STATUS-CODE PIC XX. 88 SUCCESS VALUE "00". 88 END-OF-FILE VALUE "10". 88 RECORD-NOT-FOUND VALUE "23". 88 DUPLICATE-KEY VALUE "22". 01 MENU-CHOICE PIC 9. 88 ADD-RECORD VALUE 1. 88 UPDATE-RECORD VALUE 2. 88 DELETE-RECORD VALUE 3. 88 DISPLAY-RECORD VALUE 4. 88 LIST-ALL VALUE 5. 88 EXIT-PROGRAM VALUE 9. 01 CONTINUE-FLAG PIC X VALUE "Y". 88 EXIT-APP VALUE "N". PROCEDURE DIVISION. MAIN-PROCESS. PERFORM INITIALIZATION PERFORM UNTIL EXIT-APP PERFORM DISPLAY-MENU PERFORM PROCESS-MENU-CHOICE END-PERFORM PERFORM TERMINATION STOP RUN. INITIALIZATION. OPEN I-O CUSTOMER-FILE IF NOT SUCCESS DISPLAY "Error opening file: " FILE-STATUS-CODE MOVE "N" TO CONTINUE-FLAG END-IF. DISPLAY-MENU. DISPLAY " " DISPLAY "CUSTOMER MAINTENANCE MENU" DISPLAY "1. Add Customer" DISPLAY "2. Update Customer" DISPLAY "3. Delete Customer" DISPLAY "4. Display Customer" DISPLAY "5. List All Customers" DISPLAY "9. Exit" DISPLAY "Enter choice: " ACCEPT MENU-CHOICE. PROCESS-MENU-CHOICE. EVALUATE TRUE WHEN ADD-RECORD PERFORM ADD-CUSTOMER WHEN UPDATE-RECORD PERFORM UPDATE-CUSTOMER WHEN DELETE-RECORD PERFORM DELETE-CUSTOMER WHEN DISPLAY-RECORD PERFORM DISPLAY-CUSTOMER WHEN LIST-ALL PERFORM LIST-CUSTOMERS WHEN EXIT-PROGRAM MOVE "N" TO CONTINUE-FLAG WHEN OTHER DISPLAY "Invalid choice" END-EVALUATE. ADD-CUSTOMER. DISPLAY "Enter customer ID: " ACCEPT CUST-ID DISPLAY "Enter customer name: " ACCEPT CUST-NAME DISPLAY "Enter address: " ACCEPT CUST-ADDRESS DISPLAY "Enter balance: " ACCEPT CUST-BALANCE MOVE "A" TO CUST-STATUS WRITE CUSTOMER-RECORD INVALID KEY IF DUPLICATE-KEY DISPLAY "Customer ID " CUST-ID " already exists" ELSE DISPLAY "Error adding customer: " FILE-STATUS-CODE END-IF NOT INVALID KEY DISPLAY "Customer added successfully" END-WRITE. UPDATE-CUSTOMER. DISPLAY "Enter customer ID to update: " ACCEPT CUST-ID READ CUSTOMER-FILE INVALID KEY DISPLAY "Customer " CUST-ID " not found" NOT INVALID KEY DISPLAY "Current information:" DISPLAY " Name: " CUST-NAME DISPLAY " Address: " CUST-ADDRESS DISPLAY " Balance: " CUST-BALANCE DISPLAY "Enter new name (or ENTER to keep): " ACCEPT CUST-NAME DISPLAY "Enter new address (or ENTER to keep): " ACCEPT CUST-ADDRESS DISPLAY "Enter new balance: " ACCEPT CUST-BALANCE REWRITE CUSTOMER-RECORD INVALID KEY DISPLAY "Error updating customer: " FILE-STATUS-CODE NOT INVALID KEY DISPLAY "Customer updated successfully" END-REWRITE END-READ. DELETE-CUSTOMER. DISPLAY "Enter customer ID to delete: " ACCEPT CUST-ID READ CUSTOMER-FILE INVALID KEY DISPLAY "Customer " CUST-ID " not found" NOT INVALID KEY DISPLAY "Customer to delete:" DISPLAY " Name: " CUST-NAME DISPLAY " Balance: " CUST-BALANCE DISPLAY "Confirm deletion (Y/N): " ACCEPT CONTINUE-FLAG IF CONTINUE-FLAG = "Y" DELETE CUSTOMER-FILE RECORD INVALID KEY DISPLAY "Error deleting customer: " FILE-STATUS-CODE NOT INVALID KEY DISPLAY "Customer deleted successfully" END-DELETE ELSE DISPLAY "Deletion cancelled" END-IF END-READ. DISPLAY-CUSTOMER. DISPLAY "Enter customer ID: " ACCEPT CUST-ID READ CUSTOMER-FILE INVALID KEY DISPLAY "Customer " CUST-ID " not found" NOT INVALID KEY DISPLAY "Customer Information:" DISPLAY " ID: " CUST-ID DISPLAY " Name: " CUST-NAME DISPLAY " Address: " CUST-ADDRESS DISPLAY " Balance: " CUST-BALANCE DISPLAY " Status: " CUST-STATUS END-READ. LIST-CUSTOMERS. DISPLAY "Listing all customers..." MOVE ZEROS TO CUST-ID START CUSTOMER-FILE KEY >= CUST-ID INVALID KEY DISPLAY "Error positioning file" NOT INVALID KEY PERFORM UNTIL END-OF-FILE READ CUSTOMER-FILE NEXT AT END CONTINUE NOT AT END DISPLAY CUST-ID " - " CUST-NAME " - " CUST-BALANCE END-READ END-PERFORM END-START. TERMINATION. CLOSE CUSTOMER-FILE DISPLAY "Program terminated.".

This comprehensive example demonstrates all four record operations in a single program. It uses dynamic access mode, which allows both sequential and random operations. The program provides a menu-driven interface for adding, updating, deleting, displaying, and listing customers.

File Status Codes

Understanding file status codes is crucial for proper error handling in record operations. Here are the most common codes:

Common COBOL File Status Codes
Status CodeMeaningApplicable Operations
"00"Successful operationAll operations
"10"End of file reachedREAD (sequential)
"22"Duplicate key (record already exists)WRITE (indexed files)
"23"Record not found or invalid keyREAD, REWRITE, DELETE (random access)
"24"Boundary violation or invalid operationREWRITE (without prior READ)
"30"Permanent I/O errorAll operations
"34"Boundary violation (record too large)WRITE, REWRITE

Best Practices

Follow these best practices when working with COBOL record operations:

1. Always Check File Status

Always check the file status code after each operation to ensure it completed successfully. Don't assume operations will always succeed.

2. Use Appropriate File Modes

Choose the correct file mode for your operations:

  • Use INPUT for read-only access
  • Use OUTPUT for creating new files
  • Use EXTEND for appending to sequential files
  • Use I-O for files that need updating or deletion

3. Read Before REWRITE

Always READ a record before attempting to REWRITE it. REWRITE replaces the last record that was read, so you must read first to establish which record will be updated.

4. Handle End-of-File Conditions

When processing files sequentially, always check for end-of-file conditions using AT END or file status "10" to prevent reading past the end of the file.

5. Validate Keys Before Operations

For random access operations, validate that key values are within acceptable ranges and formats before attempting READ, WRITE, REWRITE, or DELETE operations.

6. Use Dynamic Access When Appropriate

Use dynamic access mode when your program needs both sequential and random access. This provides flexibility without requiring multiple file opens.

7. Close Files Properly

Always close files when you're done with them. This ensures all buffers are flushed and resources are released properly.

Common Errors and Solutions

Error: Attempting REWRITE Without Reading First

Problem: Trying to REWRITE a record without first reading it results in file status "24" (boundary violation).

Solution: Always READ the record you want to update before using REWRITE.

Error: Using DELETE with Sequential Files

Problem: DELETE cannot be used with sequential file organization.

Solution: Use indexed or relative file organization if you need DELETE functionality, or create a new file excluding records you want to delete.

Error: Duplicate Key on WRITE

Problem: Attempting to WRITE a record with a key that already exists results in file status "22".

Solution: Check if the record exists first using READ, or handle the INVALID KEY condition and inform the user.

Error: Reading Past End of File

Problem: Continuing to read after reaching end of file causes errors.

Solution: Always check for AT END condition or file status "10" and exit your read loop when end of file is reached.

Summary

COBOL record operations provide the foundation for file processing. Understanding READ, WRITE, REWRITE, and DELETE statements, along with their requirements for file modes and access methods, is essential for effective file management. Always handle errors properly using file status codes or exception conditions, and follow best practices to ensure reliable and maintainable code.

Remember that each operation has specific requirements:

  • READ requires INPUT or I-O mode
  • WRITE requires OUTPUT, EXTEND, or I-O mode
  • REWRITE requires I-O mode and a prior READ
  • DELETE requires I-O mode and works only with indexed or relative files

By mastering these operations and understanding their proper usage, you can build robust file processing applications in COBOL.

Test Your Knowledge

1. Which file mode is required to use the REWRITE statement?

  • INPUT mode
  • OUTPUT mode
  • I-O mode
  • EXTEND mode

2. What must you do before using REWRITE to update a record?

  • Open the file in OUTPUT mode
  • Read the record you want to update
  • Delete the old record first
  • Write a new record

3. Which record operation can be used with sequential files?

  • DELETE
  • REWRITE
  • READ and WRITE
  • All of the above

4. What file status code typically indicates a successful operation?

  • "00"
  • "10"
  • "23"
  • "99"

5. What is the difference between opening a file in OUTPUT mode vs EXTEND mode?

  • OUTPUT creates a new file, EXTEND appends to existing file
  • They are the same
  • OUTPUT is for reading, EXTEND is for writing
  • EXTEND is faster than OUTPUT

6. For which file organizations can you use the DELETE statement?

  • Sequential files only
  • Indexed and relative files
  • All file organizations
  • None - DELETE is not a COBOL statement

7. What happens when you READ a file that has reached the end?

  • The program crashes
  • The AT END condition is triggered
  • The last record is read again
  • A new file is created

8. In random access mode, how do you specify which record to READ?

  • By record number
  • By setting the record key before reading
  • By using READ NEXT
  • You cannot specify which record to read

Related Pages