REWRITE and DELETE
The two lessons before this one built an indexed file and read it. Real programs also change it: a balance moves, a customer leaves. Two verbs do that work, REWRITE and DELETE, and both need a file opened in the one mode that allows them, I-O.
OPEN I-O and REWRITE
OPEN I-O opens a file that already exists for reading and writing at the same time. REWRITE then replaces the record the last read positioned on with the contents of the record area, in the same key position:
IDENTIFICATION DIVISION.
PROGRAM-ID. RW1.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CUS-F ASSIGN TO "cus5.idx"
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS CUS-ID
FILE STATUS IS WS-STATUS.
DATA DIVISION.
FILE SECTION.
FD CUS-F.
01 CUS-REC.
05 CUS-ID PIC X(3).
05 CUS-NAME PIC X(8).
05 CUS-BAL PIC 9(5).
WORKING-STORAGE SECTION.
01 WS-STATUS PIC XX.
PROCEDURE DIVISION.
OPEN OUTPUT CUS-F.
MOVE "A10" TO CUS-ID.
MOVE "Ada" TO CUS-NAME.
MOVE 100 TO CUS-BAL.
WRITE CUS-REC.
MOVE "B20" TO CUS-ID.
MOVE "Bob" TO CUS-NAME.
MOVE 200 TO CUS-BAL.
WRITE CUS-REC.
CLOSE CUS-F.
OPEN I-O CUS-F.
DISPLAY "open i-o " WS-STATUS.
MOVE "A10" TO CUS-ID.
READ CUS-F
INVALID KEY DISPLAY "no A10"
NOT INVALID KEY DISPLAY "read " CUS-ID " " CUS-BAL
END-READ.
MOVE 150 TO CUS-BAL.
REWRITE CUS-REC.
DISPLAY "rewrite " WS-STATUS.
CLOSE CUS-F.
OPEN INPUT CUS-F.
MOVE "A10" TO CUS-ID.
READ CUS-F
INVALID KEY DISPLAY "no A10"
NOT INVALID KEY DISPLAY "after " CUS-ID " " CUS-BAL
END-READ.
CLOSE CUS-F.
STOP RUN.
cobc -x -free -Wall -o rw1 rw1.cob
./rw1
open i-o 00
read A10 00100
rewrite 00
after A10 00150
The record kept its key and changed its balance, and the second program read the changed value back. The READ before the rewrite is not decoration: the verb replaces the record the file is positioned on. GnuCOBOL is lenient here as well, and a REWRITE with no read before it, in dynamic mode, replaces the record whose key is in the record area and returns 00; the standard asks for the successful read, so the code above does it.
A key that changes
The record key is written into the index of the file, so a REWRITE that carries a different key is a move inside the index, and a file read sequentially refuses it with status 21, the same sequence error the indexed files lesson showed for a WRITE:
IDENTIFICATION DIVISION.
PROGRAM-ID. RW2.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CUS-F ASSIGN TO "cus8.idx"
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS CUS-ID
FILE STATUS IS WS-STATUS.
DATA DIVISION.
FILE SECTION.
FD CUS-F.
01 CUS-REC.
05 CUS-ID PIC X(3).
05 CUS-NAME PIC X(8).
WORKING-STORAGE SECTION.
01 WS-STATUS PIC XX.
PROCEDURE DIVISION.
OPEN OUTPUT CUS-F.
MOVE "A10" TO CUS-ID.
MOVE "Ada" TO CUS-NAME.
WRITE CUS-REC.
CLOSE CUS-F.
OPEN I-O CUS-F.
MOVE "A10" TO CUS-ID.
READ CUS-F
NOT INVALID KEY DISPLAY "read " CUS-ID
END-READ.
MOVE "Z90" TO CUS-ID.
REWRITE CUS-REC
INVALID KEY DISPLAY "rewrite failed " WS-STATUS
NOT INVALID KEY DISPLAY "rewrite " WS-STATUS
END-REWRITE.
CLOSE CUS-F.
OPEN INPUT CUS-F.
PERFORM UNTIL WS-STATUS = "10"
READ CUS-F NEXT
AT END MOVE "10" TO WS-STATUS
NOT AT END DISPLAY "key " CUS-ID
END-READ
END-PERFORM.
CLOSE CUS-F.
STOP RUN.
cobc -x -free -Wall -o rw2 rw2.cob
./rw2
read A10
rewrite failed 21
key A10
The rewrite was refused and the record stayed under its old key. A program that really has to change a key deletes the old record and writes the new one, and does both inside one transaction of its own making.
DELETE
DELETE removes the record the file is positioned on, and the next read of that key returns status 23, "record not found":
IDENTIFICATION DIVISION.
PROGRAM-ID. DEL1.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CUS-F ASSIGN TO "cus6.idx"
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS CUS-ID
FILE STATUS IS WS-STATUS.
DATA DIVISION.
FILE SECTION.
FD CUS-F.
01 CUS-REC.
05 CUS-ID PIC X(3).
05 CUS-NAME PIC X(8).
WORKING-STORAGE SECTION.
01 WS-STATUS PIC XX.
01 WS-COUNT PIC 9(2) VALUE 0.
01 WS-N PIC 9(3).
PROCEDURE DIVISION.
OPEN OUTPUT CUS-F.
PERFORM VARYING WS-N FROM 3 BY 1 UNTIL WS-N > 5
MOVE WS-N TO CUS-ID
MOVE "x" TO CUS-NAME
WRITE CUS-REC
END-PERFORM.
CLOSE CUS-F.
OPEN I-O CUS-F.
MOVE "004" TO CUS-ID.
READ CUS-F
INVALID KEY DISPLAY "no 004"
NOT INVALID KEY DISPLAY "read " CUS-ID
END-READ.
DELETE CUS-F
INVALID KEY DISPLAY "delete failed " WS-STATUS
NOT INVALID KEY DISPLAY "deleted " CUS-ID
END-DELETE.
MOVE "004" TO CUS-ID.
READ CUS-F
INVALID KEY DISPLAY "after delete " WS-STATUS
END-READ.
CLOSE CUS-F.
OPEN INPUT CUS-F.
PERFORM UNTIL WS-STATUS = "10"
READ CUS-F NEXT
AT END MOVE "10" TO WS-STATUS
NOT AT END
ADD 1 TO WS-COUNT
DISPLAY "next " CUS-ID
END-READ
END-PERFORM.
DISPLAY "records " WS-COUNT.
CLOSE CUS-F.
STOP RUN.
cobc -x -free -Wall -o del1 del1.cob
./del1
read 004
deleted 004
after delete 23
next 003
next 005
records 02
Three records were written, one was read, one was deleted, and a walk of the file found two. The DELETE was given no KEY IS because the read had already positioned the file on record 004.
GnuCOBOL is lenient here: with the record area holding a key and an OPEN I-O in dynamic mode, a DELETE with no read before it deletes the record of that key and returns 00, and the next read of the same key returns 23. The standard asks for a successful READ or START first, and the code above does it, so it is portable as well as short.
An update pass
The pattern of a real update is one pass over the file under OPEN I-O, with a READ ... NEXT and a REWRITE only for the records a condition names. The file stays open, so nothing is written twice:
IDENTIFICATION DIVISION.
PROGRAM-ID. UPD.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CUS-F ASSIGN TO "cus9.idx"
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS CUS-ID
FILE STATUS IS WS-STATUS.
DATA DIVISION.
FILE SECTION.
FD CUS-F.
01 CUS-REC.
05 CUS-ID PIC X(3).
05 CUS-BAL PIC 9(3).
WORKING-STORAGE SECTION.
01 WS-STATUS PIC XX.
01 WS-CHANGED PIC 9(2) VALUE 0.
PROCEDURE DIVISION.
OPEN OUTPUT CUS-F.
MOVE "A10" TO CUS-ID.
MOVE 100 TO CUS-BAL.
WRITE CUS-REC.
MOVE "B20" TO CUS-ID.
MOVE 050 TO CUS-BAL.
WRITE CUS-REC.
MOVE "C30" TO CUS-ID.
MOVE 075 TO CUS-BAL.
WRITE CUS-REC.
CLOSE CUS-F.
OPEN I-O CUS-F.
PERFORM UNTIL WS-STATUS = "10"
READ CUS-F NEXT
AT END MOVE "10" TO WS-STATUS
NOT AT END
IF CUS-BAL < 100
ADD 10 TO CUS-BAL
REWRITE CUS-REC
ADD 1 TO WS-CHANGED
DISPLAY "raised " CUS-ID " to " CUS-BAL
END-IF
END-READ
END-PERFORM.
DISPLAY "changed " WS-CHANGED.
CLOSE CUS-F.
OPEN INPUT CUS-F.
MOVE "A10" TO CUS-ID.
READ CUS-F
NOT INVALID KEY DISPLAY "recheck " CUS-ID " " CUS-BAL
END-READ.
MOVE "B20" TO CUS-ID.
READ CUS-F
NOT INVALID KEY DISPLAY "recheck " CUS-ID " " CUS-BAL
END-READ.
MOVE "C30" TO CUS-ID.
READ CUS-F
NOT INVALID KEY DISPLAY "recheck " CUS-ID " " CUS-BAL
END-READ.
CLOSE CUS-F.
STOP RUN.
cobc -x -free -Wall -o upd upd.cob
./upd
raised B20 to 060
raised C30 to 085
changed 02
recheck A10 100
recheck B20 060
recheck C30 085
The recheck reads are there because a rewrite that quietly fails leaves a program that reports success. Reading the three keys back after the pass is the cheapest proof that the pass did what its counter says, and the ADD 1 TO WS-CHANGED beside every REWRITE is the pattern the sort and capstone lessons use for their own counts.
The next lesson keeps a record under a number instead of a key.
Exercises
- Run rw1 a second time without deleting cus5.idx. Which status does the OPEN OUTPUT return, and what does READ find afterwards?
- Change the key of a record with a DELETE and a WRITE inside the same OPEN I-O. Print the status of both verbs.
- Delete the record with key "009" in del1. What does the INVALID KEY branch print?
- Change upd so that it also prints the number of records it looked at, and compare it with the number it changed.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which OPEN mode allows REWRITE and DELETE?
Answer
I-O
Both verbs change a record already in the file, and only I-O opens a file for reading and writing at once.
-
Which record does REWRITE replace?
Answer
The record of the last successful read or START
The file is positioned by the read, and the verb replaces that record with the record area.
-
What status did the rewritten record with a new key return?
Answer
21, sequence error, and the record kept its key
The run printed rewrite failed 21 and a walk of the file still found A10.
-
What status does a read of a deleted record return?
Answer
23
The run printed after delete 23 for the key it had deleted.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT F-I ASSIGN TO "q5b.idx" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS K-F FILE STATUS IS W-S. DATA DIVISION. FILE SECTION. FD F-I. 01 R-I. 05 K-F PIC X(2). 05 V-F PIC 9(2). WORKING-STORAGE SECTION. 01 W-S PIC XX. PROCEDURE DIVISION. OPEN OUTPUT F-I. MOVE "AA" TO K-F. MOVE 10 TO V-F. WRITE R-I. CLOSE F-I. OPEN I-O F-I. MOVE "AA" TO K-F. READ F-I NOT INVALID KEY MOVE 20 TO V-F END-READ. REWRITE R-I. DISPLAY V-F " " W-S. CLOSE F-I. STOP RUN.Answer
20 00
The record was read, the value changed and the rewrite returned 00.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT F-I ASSIGN TO "q6b.idx" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS K-F FILE STATUS IS W-S. DATA DIVISION. FILE SECTION. FD F-I. 01 R-I. 05 K-F PIC X(2). WORKING-STORAGE SECTION. 01 W-S PIC XX. 01 N-C PIC 9 VALUE 0. PROCEDURE DIVISION. OPEN OUTPUT F-I. MOVE "AA" TO K-F. WRITE R-I. MOVE "BB" TO K-F. WRITE R-I. CLOSE F-I. OPEN I-O F-I. MOVE "AA" TO K-F. READ F-I NOT INVALID KEY DELETE F-I END-READ. CLOSE F-I. OPEN INPUT F-I. PERFORM UNTIL W-S = "10" READ F-I NEXT AT END MOVE "10" TO W-S NOT AT END ADD 1 TO N-C END-READ END-PERFORM. DISPLAY N-C. CLOSE F-I. STOP RUN.Answer
1
Two records were written and one was deleted, so one is left to count.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT F-I ASSIGN TO "q7b.idx" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS K-F FILE STATUS IS W-S. DATA DIVISION. FILE SECTION. FD F-I. 01 R-I. 05 K-F PIC X(2). WORKING-STORAGE SECTION. 01 W-S PIC XX. PROCEDURE DIVISION. OPEN OUTPUT F-I. CLOSE F-I. OPEN I-O F-I. MOVE "AA" TO K-F. DELETE F-I INVALID KEY DISPLAY "missing " W-S NOT INVALID KEY DISPLAY "deleted" END-DELETE. CLOSE F-I. STOP RUN.Answer
missing 23
The file is empty after the OPEN OUTPUT, so the delete finds no record and the status is 23.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT F-I ASSIGN TO "q8u.idx" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS K-F FILE STATUS IS W-S. DATA DIVISION. FILE SECTION. FD F-I. 01 R-I. 05 K-F PIC X(2). 05 V-F PIC 9. WORKING-STORAGE SECTION. 01 W-S PIC XX. PROCEDURE DIVISION. OPEN OUTPUT F-I. MOVE "AA" TO K-F. WRITE R-I. CLOSE F-I. OPEN I-O F-I. MOVE "AA" TO K-F. MOVE 7 TO V-F. REWRITE R-I INVALID KEY DISPLAY "refused " W-S NOT INVALID KEY DISPLAY "rewritten " W-S END-REWRITE. CLOSE F-I. STOP RUN.Answer
rewritten 00
The rewrite carries the key of the record it replaces, and GnuCOBOL positions the record from the record area when no read came before.
-
A program opens a file with OPEN INPUT and runs REWRITE on a record it has just read. What happens?
Answer
The rewrite leaves status 49 and the record is not replaced
Only I-O and OUTPUT accept a rewrite; measured here, the verb printed the file status 49 and the file kept its record.
-
Why does the update pass print a recheck of three keys after the loop?
Answer
A rewrite can fail without stopping the program, so the recheck proves the values changed
The status of each rewrite was not printed, and the reads are the proof that the pass did its work.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev