Relative files
A relative file sits between the two organisations of the last three lessons. It has no key of its own and no index: every record owns a number, the program names the number, and the file jumps there. For data that already has a dense number, such as a month, a branch code from 1 to 99 or a record from a previous run, it is the cheapest organisation there is.
ORGANIZATION RELATIVE
Two clauses and a key field describe the file. The key is not part of the record: it is a number the program keeps and sets before each verb.
IDENTIFICATION DIVISION.
PROGRAM-ID. REL1.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CUS-F ASSIGN TO "cus.rel"
ORGANIZATION IS RELATIVE
ACCESS MODE IS RANDOM
RELATIVE KEY IS WS-KEY
FILE STATUS IS WS-STATUS.
DATA DIVISION.
FILE SECTION.
FD CUS-F.
01 CUS-REC.
05 CUS-NAME PIC X(8).
WORKING-STORAGE SECTION.
01 WS-KEY PIC 9(3).
01 WS-STATUS PIC XX.
PROCEDURE DIVISION.
OPEN OUTPUT CUS-F.
MOVE 1 TO WS-KEY.
MOVE "Ada" TO CUS-NAME.
WRITE CUS-REC
INVALID KEY DISPLAY "write 1 failed " WS-STATUS
END-WRITE.
MOVE 2 TO WS-KEY.
MOVE "Bob" TO CUS-NAME.
WRITE CUS-REC.
MOVE 3 TO WS-KEY.
MOVE "Cy" TO CUS-NAME.
WRITE CUS-REC.
CLOSE CUS-F.
OPEN INPUT CUS-F.
MOVE 2 TO WS-KEY.
READ CUS-F
INVALID KEY DISPLAY "no record 2"
NOT INVALID KEY DISPLAY "record " WS-KEY " is " CUS-NAME
END-READ.
MOVE 4 TO WS-KEY.
READ CUS-F
INVALID KEY DISPLAY "no record 4 " WS-STATUS
NOT INVALID KEY DISPLAY "record " WS-KEY " is " CUS-NAME
END-READ.
CLOSE CUS-F.
STOP RUN.
cobc -x -free -Wall -o rel1 rel1.cob
./rel1
record 002 is Bob
no record 4 23
Record 2 came back by number, with no key field inside it. Record 4 was never written, and the read of it returned status 23, the same "record not found" that an indexed file returns for a key it does not hold. The three eight-byte records took 48 bytes on disk, so the compiler keeps eight bytes of its own beside every record.
Where the relative key lives
The key must be a field of its own, outside the record. Written inside the FD, it looks natural and the compiler refuses it:
IDENTIFICATION DIVISION.
PROGRAM-ID. BADREL.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CUS-F ASSIGN TO "bad.rel"
ORGANIZATION IS RELATIVE
ACCESS MODE IS RANDOM
RELATIVE KEY IS CUS-NUM
FILE STATUS IS WS-STATUS.
DATA DIVISION.
FILE SECTION.
FD CUS-F.
01 CUS-REC.
05 CUS-NUM PIC 9(3).
WORKING-STORAGE SECTION.
01 WS-STATUS PIC XX.
PROCEDURE DIVISION.
STOP RUN.
cobc -x -free -Wall -o bad bad.cob
bad.cob:9: error: RELATIVE KEY CUS-NUM cannot be in file record belonging to CUS-F
The reason is the meaning of the field: a key inside the record would be read from and written to the file, so a read would have to know the number before it set it. The number belongs to the program, and one PIC 9 field in WORKING-STORAGE serves every relative file the program opens.
A slot for every number
The file is a sequence of numbered slots, and it grows to the highest number used. A program that writes numbers 1 and 5 first gets slots 2, 3 and 4 as well, empty and unreadable:
IDENTIFICATION DIVISION.
PROGRAM-ID. REL2.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CUS-F ASSIGN TO "hole.rel"
ORGANIZATION IS RELATIVE
ACCESS MODE IS RANDOM
RELATIVE KEY IS WS-KEY
FILE STATUS IS WS-STATUS.
DATA DIVISION.
FILE SECTION.
FD CUS-F.
01 CUS-REC PIC X(10).
WORKING-STORAGE SECTION.
01 WS-KEY PIC 9(3).
01 WS-STATUS PIC XX.
01 WS-SLOT PIC 9(3).
PROCEDURE DIVISION.
OPEN OUTPUT CUS-F.
MOVE 1 TO WS-KEY.
MOVE "first" TO CUS-REC.
WRITE CUS-REC.
MOVE 5 TO WS-KEY.
MOVE "fifth" TO CUS-REC.
WRITE CUS-REC.
CLOSE CUS-F.
OPEN INPUT CUS-F.
PERFORM VARYING WS-SLOT FROM 1 BY 1 UNTIL WS-SLOT > 5
MOVE WS-SLOT TO WS-KEY
READ CUS-F
INVALID KEY DISPLAY "slot " WS-SLOT " " WS-STATUS
NOT INVALID KEY DISPLAY "slot " WS-SLOT " " CUS-REC
END-READ
END-PERFORM.
CLOSE CUS-F.
STOP RUN.
cobc -x -free -Wall -o rel2 rel2.cob
./rel2
slot 001 first
slot 002 23
slot 003 23
slot 004 23
slot 005 fifth
The file is 90 bytes: five slots of a ten-byte record with the eight bytes the compiler adds to each one. The three empty slots are not records with a flag, they are the absence of a record, which is why the status is the same 23 as a number past the end.
REWRITE and DELETE
The verbs of the previous lesson work here unchanged, and the number in the key field says which record they act on. A DELETE leaves a hole exactly like a number that was never written:
IDENTIFICATION DIVISION.
PROGRAM-ID. REL3.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CUS-F ASSIGN TO "upd.rel"
ORGANIZATION IS RELATIVE
ACCESS MODE IS DYNAMIC
RELATIVE KEY IS WS-KEY
FILE STATUS IS WS-STATUS.
DATA DIVISION.
FILE SECTION.
FD CUS-F.
01 CUS-REC.
05 CUS-NAME PIC X(8).
WORKING-STORAGE SECTION.
01 WS-KEY PIC 9(3).
01 WS-STATUS PIC XX.
01 WS-SLOT PIC 9(3).
PROCEDURE DIVISION.
OPEN OUTPUT CUS-F.
PERFORM VARYING WS-KEY FROM 1 BY 1 UNTIL WS-KEY > 3
MOVE "old" TO CUS-NAME
WRITE CUS-REC
END-PERFORM.
CLOSE CUS-F.
OPEN I-O CUS-F.
MOVE 2 TO WS-KEY.
READ CUS-F
NOT INVALID KEY
MOVE "new" TO CUS-NAME
REWRITE CUS-REC
DISPLAY "rewrote 2 " WS-STATUS
END-READ.
MOVE 3 TO WS-KEY.
DELETE CUS-F
NOT INVALID KEY DISPLAY "deleted 3 " WS-STATUS
END-DELETE.
CLOSE CUS-F.
OPEN INPUT CUS-F.
PERFORM VARYING WS-SLOT FROM 1 BY 1 UNTIL WS-SLOT > 3
MOVE WS-SLOT TO WS-KEY
READ CUS-F
INVALID KEY DISPLAY "slot " WS-SLOT " " WS-STATUS
NOT INVALID KEY DISPLAY "slot " WS-SLOT " " CUS-NAME
END-READ
END-PERFORM.
CLOSE CUS-F.
STOP RUN.
cobc -x -free -Wall -o rel3 rel3.cob
./rel3
rewrote 2 00
deleted 3 00
slot 001 old
slot 002 new
slot 003 23
Access mode DYNAMIC is what allows a random read and a sequential walk in the same open. With ACCESS MODE IS SEQUENTIAL the program starts at record 1 and moves forward one number at a time, and with RANDOM it goes straight to the number it names; the file itself is the same either way.
A relative file is the right answer when the numbers run from 1 to something small and dense. A customer number of nine digits would make it a file of a thousand million slots, and that is what an indexed file is for.
Exercises
- Write records 1 to 3 and then read number 0. Which status comes back, and why is it not 23?
- Add a sixth slot to rel2. How many bytes does the file have afterwards?
- Change REL3 to ACCESS MODE IS SEQUENTIAL and keep the random read of record 2. What does the program do?
- Give the relative key a PIC 9(1) and write record 12. What does the compiler warn about?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Where does the RELATIVE KEY field live?
Answer
In WORKING-STORAGE, outside the file record
The compiler refused a key inside the record: RELATIVE KEY CUS-NUM cannot be in file record belonging to CUS-F.
-
What does a relative file do when the program writes record 5 into an empty file?
Answer
It creates slots 1 to 4 as well, empty, and the file grows to five slots
The run read slots 2, 3 and 4 with status 23 and the file held five slots of 18 bytes.
-
Which status does a read of a slot that was never written return?
Answer
23
The hole program printed 23 for slots 2, 3 and 4.
-
Which access mode lets one open do a random read and then a walk from slot 1?
Answer
DYNAMIC
The update program read by number and then walked all three slots under one OPEN I-O with DYNAMIC.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT F-R ASSIGN TO "q5r.rel" ORGANIZATION IS RELATIVE ACCESS MODE IS RANDOM RELATIVE KEY IS W-K FILE STATUS IS W-S. DATA DIVISION. FILE SECTION. FD F-R. 01 R-R PIC X(4). WORKING-STORAGE SECTION. 01 W-K PIC 9(2). 01 W-S PIC XX. PROCEDURE DIVISION. OPEN OUTPUT F-R. MOVE 3 TO W-K. MOVE "xyz" TO R-R. WRITE R-R. CLOSE F-R. OPEN INPUT F-R. MOVE 3 TO W-K. READ F-R INVALID KEY DISPLAY "missing" NOT INVALID KEY DISPLAY R-R END-READ. CLOSE F-R. STOP RUN.Answer
xyz
The record was written in the slot of number 3 and read back from it.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT F-R ASSIGN TO "q6r.rel" ORGANIZATION IS RELATIVE ACCESS MODE IS RANDOM RELATIVE KEY IS W-K FILE STATUS IS W-S. DATA DIVISION. FILE SECTION. FD F-R. 01 R-R PIC X(4). WORKING-STORAGE SECTION. 01 W-K PIC 9(2). 01 W-S PIC XX. 01 N-C PIC 9(2) VALUE 0. 01 I-S PIC 9(2). PROCEDURE DIVISION. OPEN OUTPUT F-R. MOVE 1 TO W-K. WRITE R-R. MOVE 4 TO W-K. WRITE R-R. CLOSE F-R. OPEN INPUT F-R. PERFORM VARYING I-S FROM 1 BY 1 UNTIL I-S > 4 MOVE I-S TO W-K READ F-R INVALID KEY CONTINUE NOT INVALID KEY ADD 1 TO N-C END-READ END-PERFORM. DISPLAY N-C. CLOSE F-R. STOP RUN.Answer
2
Two records were written, so two of the four slots hold a record and the other two return 23.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT F-R ASSIGN TO "q7r.rel" ORGANIZATION IS RELATIVE ACCESS MODE IS RANDOM RELATIVE KEY IS W-K FILE STATUS IS W-S. DATA DIVISION. FILE SECTION. FD F-R. 01 R-R PIC X(6). WORKING-STORAGE SECTION. 01 W-K PIC 9(2). 01 W-S PIC XX. PROCEDURE DIVISION. OPEN OUTPUT F-R. MOVE 1 TO W-K. MOVE "aa" TO R-R. WRITE R-R. CLOSE F-R. OPEN I-O F-R. MOVE 2 TO W-K. DELETE F-R INVALID KEY DISPLAY "none " W-S NOT INVALID KEY DISPLAY "gone" END-DELETE. CLOSE F-R. STOP RUN.Answer
none 23
Only slot 1 holds a record, so the delete of slot 2 finds nothing and returns 23.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT F-R ASSIGN TO "q8r.rel" ORGANIZATION IS RELATIVE ACCESS MODE IS RANDOM RELATIVE KEY IS W-K FILE STATUS IS W-S. DATA DIVISION. FILE SECTION. FD F-R. 01 R-R. 05 V-R PIC 9(2). WORKING-STORAGE SECTION. 01 W-K PIC 9(2). 01 W-S PIC XX. PROCEDURE DIVISION. OPEN OUTPUT F-R. MOVE 1 TO W-K. MOVE 10 TO V-R. WRITE R-R. CLOSE F-R. OPEN I-O F-R. MOVE 1 TO W-K. READ F-R NOT INVALID KEY MOVE 20 TO V-R REWRITE R-R END-READ. CLOSE F-R. OPEN INPUT F-R. MOVE 1 TO W-K. READ F-R NOT INVALID KEY DISPLAY V-R END-READ. CLOSE F-R. STOP RUN.Answer
20
The rewrite replaced the record in slot 1 and the last read returned the new value.
-
Why is a relative file a poor choice for a nine-digit customer number?
Answer
The file allocates a slot up to the highest number used, which would be huge
The key field itself may hold nine digits; the cost is the file, which is as long as the largest number.
-
A relative file is open with ACCESS MODE SEQUENTIAL, and the program sets the key to 5 and runs READ. What happens?
Answer
The read returns the next record of the walk, record 1, and the key field shows 01
Measured here: the file held records 1 and 2 and the read printed got aa 01, so the verb ignored the key and walked.
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