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 (changed, updated).
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 in it
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 (changed, updated).
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 in it