MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL Relative File Operations

Relative file operations in COBOL involve working with RRDS (Relative Record Data Set) files that provide direct access to records by relative record number (position). Each record has a fixed position identified by its relative record number (1, 2, 3, etc.), allowing you to read, write, update, or delete records by specifying the position. Understanding relative file operations is essential for working with lookup tables, fixed-size data structures, and applications where position-based access is appropriate in mainframe COBOL programs.

What are Relative Files?

Relative files (RRDS) provide position-based access:

  • Position-based access: Records accessed by relative record number (1, 2, 3, etc.)
  • Fixed positions: Each record has a fixed slot identified by position
  • Direct access: Can access any record directly by its position
  • Random or sequential: Supports both access modes
  • Updates and deletes: Can modify and remove records
  • Fixed record length: All records must be the same length

Relative files are useful when you know the record position or need array-like access to disk-based data.

Defining Relative Files

Define relative files using ORGANIZATION IS RELATIVE:

Relative 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
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT LOOKUP-FILE ASSIGN TO LOOKUP.VSAM ORGANIZATION IS RELATIVE ACCESS MODE IS RANDOM RELATIVE KEY IS RECORD-NUMBER FILE STATUS IS LOOKUP-STATUS. DATA DIVISION. FILE SECTION. FD LOOKUP-FILE. 01 LOOKUP-RECORD. 05 STATE-CODE PIC X(2). 05 STATE-NAME PIC X(20). 05 STATE-TAX-RATE PIC V999. 05 FILLER PIC X(75). *> Fixed record length WORKING-STORAGE SECTION. 01 RECORD-NUMBER PIC 9(4). 01 LOOKUP-STATUS PIC XX. 88 FILE-OK VALUE '00'. 88 RECORD-NOT-FOUND VALUE '23'.

Key components:

  • ORGANIZATION IS RELATIVE: Specifies relative file organization
  • ACCESS MODE: RANDOM for direct access, SEQUENTIAL for sequential access
  • RELATIVE KEY: The numeric field that holds the relative record number
  • Fixed record length: All records must be the same size

Reading Relative File Records

Reading records by position:

Random Access Reading

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
PROCEDURE DIVISION. MAIN-PARA. OPEN INPUT LOOKUP-FILE *> Read record at position 5 MOVE 5 TO RECORD-NUMBER READ LOOKUP-FILE INVALID KEY DISPLAY "Record not found at position: " RECORD-NUMBER NOT INVALID KEY DISPLAY "State Code: " STATE-CODE DISPLAY "State Name: " STATE-NAME DISPLAY "Tax Rate: " STATE-TAX-RATE END-READ *> Read record at position 10 MOVE 10 TO RECORD-NUMBER READ LOOKUP-FILE INVALID KEY DISPLAY "Record not found at position: " RECORD-NUMBER NOT INVALID KEY DISPLAY "Record 10: " STATE-NAME END-READ CLOSE LOOKUP-FILE STOP RUN.

For random access, set the RELATIVE KEY to the desired position, then READ. The record at that position is retrieved.

Sequential Access Reading

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
SELECT LOOKUP-FILE ASSIGN TO LOOKUP.VSAM ORGANIZATION IS RELATIVE ACCESS MODE IS SEQUENTIAL RELATIVE KEY IS RECORD-NUMBER. PROCEDURE DIVISION. MAIN-PARA. OPEN INPUT LOOKUP-FILE *> Read records sequentially (1, 2, 3, etc.) PERFORM UNTIL END-OF-FILE READ LOOKUP-FILE NEXT AT END SET END-OF-FILE TO TRUE NOT AT END DISPLAY "Position: " RECORD-NUMBER DISPLAY "State: " STATE-NAME *> Process record END-READ END-PERFORM CLOSE LOOKUP-FILE STOP RUN.

Sequential access reads records in order starting from position 1, advancing through positions 2, 3, 4, etc.

Writing to Relative Files

Writing records to specific positions:

Writing New Records

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
PROCEDURE DIVISION. MAIN-PARA. OPEN OUTPUT LOOKUP-FILE *> Write record at position 1 MOVE 1 TO RECORD-NUMBER MOVE 'TX' TO STATE-CODE MOVE 'TEXAS' TO STATE-NAME MOVE 0.0825 TO STATE-TAX-RATE WRITE LOOKUP-RECORD INVALID KEY DISPLAY "Error writing at position: " RECORD-NUMBER NOT INVALID KEY DISPLAY "Record written at position: " RECORD-NUMBER END-WRITE *> Write record at position 2 MOVE 2 TO RECORD-NUMBER MOVE 'CA' TO STATE-CODE MOVE 'CALIFORNIA' TO STATE-NAME MOVE 0.0725 TO STATE-TAX-RATE WRITE LOOKUP-RECORD END-WRITE CLOSE LOOKUP-FILE STOP RUN.

Set the RELATIVE KEY to the desired position, prepare the record data, then WRITE. The record is stored at that position.

Updating Relative File Records

Updating records requires reading first, then rewriting:

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
PROCEDURE DIVISION. MAIN-PARA. OPEN I-O LOOKUP-FILE *> Read record at position 5 MOVE 5 TO RECORD-NUMBER READ LOOKUP-FILE INVALID KEY DISPLAY "Record not found at position: " RECORD-NUMBER NOT INVALID KEY *> Modify record data MOVE 0.0900 TO STATE-TAX-RATE MOVE 'UPDATED' TO STATE-NAME *> Write updated record back REWRITE LOOKUP-RECORD INVALID KEY DISPLAY "Error updating record" NOT INVALID KEY DISPLAY "Record updated at position: " RECORD-NUMBER END-REWRITE END-READ CLOSE LOOKUP-FILE STOP RUN.

The update process: read the record, modify data in memory, then REWRITE to write it back to the same position.

Deleting Relative File Records

Deleting records removes them from specific positions:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PROCEDURE DIVISION. MAIN-PARA. OPEN I-O LOOKUP-FILE *> Delete record at position 10 MOVE 10 TO RECORD-NUMBER DELETE LOOKUP-FILE RECORD INVALID KEY DISPLAY "Record not found at position: " RECORD-NUMBER NOT INVALID KEY DISPLAY "Record deleted at position: " RECORD-NUMBER END-DELETE CLOSE LOOKUP-FILE STOP RUN.

DELETE removes the record at the specified position. The position becomes available for reuse, but other record positions remain unchanged.

Relative vs Indexed Files

Understanding when to use relative vs indexed files:

Relative Files vs Indexed Files
AspectRelative FilesIndexed Files
Access methodBy position (1, 2, 3, etc.)By key value
Key typeRELATIVE KEY (numeric position)RECORD KEY (data value)
Record orderBy positionSorted by key
Use whenPosition is meaningful, lookup tablesKey-based access, meaningful keys
FlexibilityLess flexible, requires knowing positionMore flexible, access by meaningful keys
ComplexitySimplerMore complex with indexes

Complete Relative File Example

Complete example demonstrating relative file operations:

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
IDENTIFICATION DIVISION. PROGRAM-ID. RELATIVE-FILE-DEMO. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT STATE-LOOKUP ASSIGN TO STATE.VSAM ORGANIZATION IS RELATIVE ACCESS MODE IS RANDOM RELATIVE KEY IS STATE-POSITION FILE STATUS IS STATE-STATUS. DATA DIVISION. FILE SECTION. FD STATE-LOOKUP. 01 STATE-RECORD. 05 STATE-CODE PIC X(2). 05 STATE-NAME PIC X(20). 05 STATE-POPULATION PIC 9(8). 05 FILLER PIC X(102). *> Total 132 bytes WORKING-STORAGE SECTION. 01 STATE-POSITION PIC 9(4). 01 STATE-STATUS PIC XX. 88 FILE-OK VALUE '00'. 88 RECORD-NOT-FOUND VALUE '23'. PROCEDURE DIVISION. MAIN-PARA. DISPLAY "=== RELATIVE FILE DEMONSTRATION ===" PERFORM INITIALIZE-FILE PERFORM ADD-RECORDS PERFORM READ-RECORDS PERFORM UPDATE-RECORD PERFORM DELETE-RECORD PERFORM FINALIZE STOP RUN. INITIALIZE-FILE. DISPLAY "Initializing state lookup file..." OPEN OUTPUT STATE-LOOKUP IF NOT FILE-OK DISPLAY "Error opening file: " STATE-STATUS STOP RUN END-IF. ADD-RECORDS. DISPLAY "Adding state records..." *> Add Texas at position 1 MOVE 1 TO STATE-POSITION MOVE 'TX' TO STATE-CODE MOVE 'TEXAS' TO STATE-NAME MOVE 29000000 TO STATE-POPULATION WRITE STATE-RECORD INVALID KEY DISPLAY "Error writing position 1" END-WRITE *> Add California at position 2 MOVE 2 TO STATE-POSITION MOVE 'CA' TO STATE-CODE MOVE 'CALIFORNIA' TO STATE-NAME MOVE 39000000 TO STATE-POPULATION WRITE STATE-RECORD END-WRITE *> Add New York at position 3 MOVE 3 TO STATE-POSITION MOVE 'NY' TO STATE-CODE MOVE 'NEW YORK' TO STATE-NAME MOVE 20000000 TO STATE-POPULATION WRITE STATE-RECORD END-WRITE CLOSE STATE-LOOKUP DISPLAY "Records added successfully". READ-RECORDS. DISPLAY "Reading state records..." OPEN INPUT STATE-LOOKUP *> Read position 1 MOVE 1 TO STATE-POSITION READ STATE-LOOKUP INVALID KEY DISPLAY "Position 1 not found" NOT INVALID KEY DISPLAY "Position 1: " STATE-NAME " (" STATE-CODE ")" END-READ *> Read position 2 MOVE 2 TO STATE-POSITION READ STATE-LOOKUP NOT INVALID KEY DISPLAY "Position 2: " STATE-NAME " (" STATE-CODE ")" END-READ CLOSE STATE-LOOKUP. UPDATE-RECORD. DISPLAY "Updating record at position 2..." OPEN I-O STATE-LOOKUP MOVE 2 TO STATE-POSITION READ STATE-LOOKUP NOT INVALID KEY *> Update population ADD 1000000 TO STATE-POPULATION REWRITE STATE-RECORD NOT INVALID KEY DISPLAY "Record updated: " STATE-NAME END-REWRITE END-READ CLOSE STATE-LOOKUP. DELETE-RECORD. DISPLAY "Deleting record at position 3..." OPEN I-O STATE-LOOKUP MOVE 3 TO STATE-POSITION DELETE STATE-LOOKUP RECORD INVALID KEY DISPLAY "Record not found at position 3" NOT INVALID KEY DISPLAY "Record deleted at position 3" END-DELETE CLOSE STATE-LOOKUP. FINALIZE. DISPLAY "=== DEMONSTRATION COMPLETE ===".

Best Practices for Relative Files

Follow these best practices:

  • Use fixed record length: Ensure all records are the same size
  • Validate positions: Check that relative record numbers are within valid range
  • Handle INVALID KEY: Always handle INVALID KEY for random access operations
  • Use meaningful positions: Assign positions that make sense for your application
  • Check file status: Always check FILE STATUS after operations
  • Close files properly: Always close relative files when done
  • Document position meanings: Comment code to explain what each position represents
  • Handle empty positions: Be aware that positions may be empty (deleted records)
  • Consider sequential access: Use sequential access when processing all records
  • Test boundary conditions: Test with position 1, maximum position, and invalid positions

Common Relative File Patterns

Pattern 1: Lookup Table

cobol
1
2
3
4
5
6
7
8
*> Use relative file as lookup table MOVE LOOKUP-INDEX TO RECORD-NUMBER READ LOOKUP-FILE INVALID KEY DISPLAY "Lookup value not found" NOT INVALID KEY *> Use LOOKUP-RECORD data END-READ

Pattern 2: Processing All Records

cobol
1
2
3
4
5
6
7
8
9
10
*> Process all records sequentially OPEN INPUT LOOKUP-FILE PERFORM UNTIL END-OF-FILE READ LOOKUP-FILE NEXT AT END SET END-OF-FILE TO TRUE NOT AT END *> Process record at RECORD-NUMBER END-READ END-PERFORM CLOSE LOOKUP-FILE

Pattern 3: Update by Position

cobol
1
2
3
4
5
6
7
8
*> Update record at known position MOVE UPDATE-POSITION TO RECORD-NUMBER READ LOOKUP-FILE NOT INVALID KEY *> Modify data MOVE NEW-DATA TO RECORD-FIELDS REWRITE LOOKUP-RECORD END-READ

Explain Like I'm 5: Relative Files

Think of relative files like a row of numbered mailboxes:

  • Relative record number is like the mailbox number - mailbox 1, mailbox 2, mailbox 3
  • Reading a record is like opening a specific mailbox by its number to see what's inside
  • Writing a record is like putting something in a specific numbered mailbox
  • Updating a record is like taking something out of a mailbox, changing it, and putting it back in the same mailbox
  • Deleting a record is like emptying a mailbox - the mailbox number stays, but it's empty

So relative files are like a row of numbered mailboxes where you can go directly to any mailbox by its number!

Practice Exercises

Complete these exercises to reinforce your understanding:

Exercise 1: Basic Relative File

Create a program that writes 10 records to a relative file at positions 1 through 10, then reads them back and displays each record.

Exercise 2: Lookup Table

Create a relative file that acts as a lookup table. Write state codes and names to specific positions, then create a lookup function that finds a state by position.

Exercise 3: Update Operations

Create a program that reads a record from a relative file, modifies specific fields, and uses REWRITE to update it. Handle errors appropriately.

Exercise 4: Sequential Processing

Create a program that processes all records in a relative file sequentially using READ NEXT, displaying each record's position and data.

Exercise 5: Delete and Reuse

Create a program that deletes a record from a relative file, then writes a new record to that same position, demonstrating position reuse.

Test Your Knowledge

1. What organization type is used for relative files?

  • ORGANIZATION IS INDEXED
  • ORGANIZATION IS RELATIVE
  • ORGANIZATION IS SEQUENTIAL
  • ORGANIZATION IS RANDOM

2. What key is used to access relative file records?

  • RECORD KEY
  • RELATIVE KEY
  • PRIMARY KEY
  • INDEX KEY

3. What is the first record position in a relative file?

  • 0
  • 1
  • First available
  • Depends on the file

4. How do you read a record at position 5 in a relative file?

  • MOVE 5 TO RECORD-KEY, then READ
  • MOVE 5 TO RELATIVE-KEY, then READ
  • READ with position 5
  • READ NEXT 5 times

5. Can you use sequential access with relative files?

  • No, only random access
  • Yes, using READ NEXT
  • Only with special commands
  • Only for reading

6. What happens when you delete a record in a relative file?

  • All records shift positions
  • The position becomes available but other positions unchanged
  • The file is reorganized
  • You cannot delete from relative files

Related Concepts

Related Pages