In this file organization, the records of the file are stored one after another both physically and logically. That is, record with sequence number 16 is located just after the 15th record.
In contrast to SEQ files, records of a RELATIVE file
can be accessed by specifying the record sequence number in the
READ statement (the KEY) and without needing to
read all the previous records.
It is the programmer's responsibility to take care
of the record sizes in files. You must be careful when declaring
record structures for files. Any mistake you make in record sizes
will cause your program to read/write erroneous information. This
is especially dangerous if the file contents are being altered
Please note that you must provide the record structure
to have a special field allocated to contain the KEY, that is
the record sequence number.
RELATIVE files can have RANDOM or SEQUENTIAL ACCESS
MODEs. If the ACCESS MODE is declared to be RANDOM in
the corresponding SELECT statement, the program can read the record
with key value 2345 and then the record with key=3, then record
2344 etc etc. In short, one can access any record of a relative
file, in any order, provided that the KEY value is specified before
the READ statement is executed.
If the ACCESS MODE is SEQUENTIAL, that means the
records of the file will be accesses in their physical sequential
order (just like SEQUENTIAL and LINE SEQUENTIAL files) and no
specific KEY value be given for the READ statements; but instead,
the NEXT clause will appear in READ statements meaning "Go
get the record with the next consecutive key value.
SELECT MYFILE ASSIGN TO DISK "MYFILE.DAT" ORGANIZATION IS RELATIVE ACCESS MODE IS RANDOM RECORD KEY IS M-IDNO. SELECT MYFILE-2 ASSIGN TO DISK "C:\DATADIR\MYFILE2.TXT" ORGANIZATION IS RELATIVE ACCESS MODE IS SEQUENTIAL RECORD KEY IS M-IDNO.
In the FILE-SECTION, you must provide fields for
the KEY, the numerical M-IDNO in this example, in the FD block
of the RELATIVE file;
FD MYFILE. 01 MYFILE-REC. 02 M-IDNO PIC 9999. 02 M-NAME PIC X(16). 02 M-SURNAME PIC X(16). 02 M-BIRTHDATE. 03 M-BD-YEAR PIC 9999. 03 M-BD-MONTH PIC 99. 03 M-BD-DAY PIC 99.
Note : Since the
key field will contain integer, record sequence numbers; you should
declare the field to be numerical and take care that it is wide
enough to carry all possible values for the key value. For example,
if your file is expected to have 200,000 records in it, the key
field should declared at least 6 bytes ( PIC 999999 ) so that
it can hold the values 1 through 200,000. A key field declaration
of "PIC 999" would let use a file of max 999 records
Like all files, relative files must be OPENed
before they can be processes and CLOSEd when they are not
Relative files can be opened for :
Opening a relative file for OUTPUT means that
the program will only issue WRITE statements to a NON-EXISTING
and JUST CREATED file. Therefore, when you open a file for OUTPUT,
COBOL assumes that the file does not exist and try to
create a new one. IF THE FILE EXISTS, ITS CONTENTS WILL BE CLEARED
WHEN OPENED AND ASSUMED TO BE A BRAND NEW FILE INTO WHICH
RECORDS WILL BE ADDED. You should be very careful when opening
a file for OUTPUT. One small mistake and all your valuable
records are lost forever.
Once you open a relative file in Output mode, you
are expected to write records in the INCREASING and CONSECUTIVE
order of keys. That is, before writing the record with key 98
(98th record) you should first write the first 97 records(.
Opening a relative file for INPUT means that
the program will only issue READ statements to an EXISTING file.
Therefore, when you open a file for INPUT, COBOL assumes that
the file exists and try to access it. IF THE FILE DOES
NOT EXIST, AN ERROR MESSAGE WILL BE ISSUED INDICATING THAT THE
MENTIONED FILE COULD NOT BE FOUND.
If you have declared the ACCESS MODE to be RANDOM,
before each READ statement, you are supposed to move a valid KEY
value to the record field variable that is declared as the RECORD
OPEN INPUT MYFILE. .... MOVE 23 TO M-IDNO. READ MYFILE.
Good programmers should take precautions in their
program to avoid error messages and subsequent abnormal terminations
is an INVALID value for the record key specified before the READ
The INVALID KEY clause handles this in COBOL.
MOVE 2300 TO M-IDNO. READ MYFILE INVALID KEY PERFORM OUT-OF-RANGE.
The INVALID KEY condition raises if the value in
M-IDNO is zero, negative or has a value greater than the total
number of records in the file.
If you have declared the ACCESS MODE as SEQUENTIAL,
you should use the NEXT clause in the READ statement. Like
READ MYFILE NEXT AT END PEFORM NO-MORE-RECORDS.
When you are finished with a file you must CLOSE
CLOSE NEW-FILE. CLOSE OLD-FILE NEW-FILE.
You can close more than one files with a single
CLOSE statement. When a COBOL program terminates, all files mentioned
in the program are automatically closed; therefore you will not
get an error message for those files you forget to close or do
not close at all. Please note that relying on COBOL to close your
files is not a proper programming style. Although not absolutely
necessary, you should close your files when you are done with
In order to rewind a relative file with ACCESS
MODE RANDOM, you do not need to rewind it when you need to go
the first record. Just move 1 to the record key and issue a READ
Once you open a relative file, you can use READ
or WRITE statements to read or append records to this file. You
cannot use a READ statement for a file opened for OUTPUT and
similarly you cannot use a WRITE statement for a file you have
opened for INPUT.
A typical READ statement for RANDOM access mode looks
MOVE 123 TO M-IDNO. READ MY-FILE.
A good programmer must check whether the READ statement
could find a record to read successfully. That means; the programmer
must check whether there were any records with the indicated key
value when the statement was executed. The situation can be checked
with the following construct :
READ MY-FILE INVALID KEY PERFORM NO-RECORDS-FOUND.
The construct tells the COBOL compiler to execute
the paragraph labeled NO-RECORDS-FOUND when the READ statement
cannot find any record with the key value stored in the key field
Once your program EXECUTES a successful READ statement,
the information in the data record that was just brought into
memory will be available in the corresponding variables mentioned
in your record description declaration (the FD block).
When you want to put records in a relative file,
you have to open it for OUTPUT. The COBOL statement that is used
to put records into a file is the WRITE statement. The important
pointa that you should be careful with are that,
UPDATING RECORDS OF A RELATIVE FILE
Sometimes you need to make some small changes in
the contents of a file. Of course it possible to create a brand
new file with the new, modified contents but this is not practical.
COBOL provides you with an OPEN I-O mode with which you can modify
only the required record in a file.
In other words, there is another file opening mode;
the I-O mode; and another special REWRITE statement. Suppose
that you want to change the surname field of the 20th
record of a relative file (originally "AYFER") into
The program that you can write could read like
SELECT MYFILE ASSIGN TO DISK "MYFILE.DAT" ORGANIZATION IS RELATIVE ACCESS MODE IS RANDOM RECORD KEY IS M-IDNO. .... FD MYFILE. 01 MYFILE-REC. 02 M-IDNO PIC 9999. 02 M-NAME PIC X(16). 02 M-SURNAME PIC X(16). 02 M-BIRTHDATE. 03 M-BD-YEAR PIC 9999. 03 M-BD-MONTH PIC 99. 03 M-BD-DAY PIC 99. PROCEDURE DIVISION. .... OPEN I-O MYFILE. .... MOVE 20 TO M-IDNO. READ MYFILE INVALID KEY PERFORM KEY-ERROR. * * TO MAKE SURE THAT WE ARE ON THE CORRECT RECORD * IF M-SURNAME NOT = "AYFER" DISPLAY "WRONG RECORD" DISPLAY "RECORD NOT UPDATED" CLOSE MYFILE STOP RUN. MOVE "AYFEROGLU" TO M-SURNAME. REWRITE MYFILE-REC. ..... CLOSE MYFILE. .....
It is NOT possible to delete records of a relative
file. If you do not want a specific record
to be kept in a relative file any more, all you can do is to
modify the contents of the record so that it contains some special
values that your program will recognize as deleted (remember
to open the file in I-O mode and REWRITE a new record).
Can be only used in conjunction with consecutive
numerical keys. If you key value range is 1-1000, you must have
records for all 1000 possible keys, that is, your file must have
exactly 1000 records. If, for example, you are planning to use
student ID numbers as numerical key values and the student id
numbers are like 9653421232 (10 digits), you file must contain
9,999,999,999 records and this doesn't make sense. Of course,
you should modify the student ID number structure to start from1
and extend till a few thousand.
This disadvantage (only numerical and consecutive
values for the key value) is overcome with a completely different
file structure, nameky the INDEXED SEQUENTIAL FILE.