Relative file -
file processing verbs |
Introduction |
Direct access files can support a far greater range of operations than
Sequential files. Just like Sequential files, direct access files support
the OPEN, CLOSE, READ and WRITE operations. But in
addition to these, direct access files also support the DELETE, REWRITE and START operations.
In this section we examine these new operations and any changes to the
operations we are already familiar with.
|
The Invalid Key clause |
When any of the file processing verbs are used for direct access, the
INVALID KEY clause must be used unless declaratives
have been specified.
When the INVALID KEY clause is specified, any I-O
error, such as attempting to read a record that does not exist or write a
record that already exists, will activate the clause and cause the
statement block following it to be executed.
|
OPEN/CLOSE verbs |
The syntax for the CLOSE is the same for all file
organizations.
The full syntax for the OPEN verbs is shown below. Note the
new I-O entry. This is used with direct access files when we intend to
update or both read and write to the file.

Notes If the file is opened for input then
only READ and START will be
allowed.
If the file is opened for output then only
WRITE will be allowed.
If the file is opened for I-O then READ, WRITE,
START, REWRITE and DELETE will be
allowed.
If OPEN INPUT is used, and the file does
not possess the OPTIONAL tag, then the file must exist
or the OPEN will fail.
If OPEN OUTPUT or I-O is used then the
file will be created if it does not already exist as long as the file
possesses the OPTIONAL tag.
|
The READ verb |
When a Relative file has an ACCESS MODE of SEQUENTIAL, the format of the READ is
the same as for Sequential files, but when the ACCESS
MODE is DYNAMIC or RANDOM,
the READ has a different format.
|
Reading using a key |

Operation To read a record directly from a Relative file
- The key value must be placed in the KeyName data item (the
KeyName data item is the area of storage identified as the
relative key in the RECORD KEY IS phrase of the
SELECT and ASSIGN clause).
- Then the READ must be executed.
When the READ is executed, the record with the
Relative Record Number equal to the present value of the relative key
(i.e.KeyName) will be read into the file's record buffer (defined
in the FD entry).
If the record does not exist the INVALID KEY clause
will activate and the statement block following the clause will be
executed.
After the record has been read the next record pointer will be pointing
to the next record in the file.
Notes The file must have an ACCESS MODE
declaration for the file specifying an ACCESS MODE
DYNAMIC or RANDOM.
The file must be opened for I-O or INPUT.
|
Reading Sequentially |
When the ACCESS MODE is DYNAMIC
and we wish to read the file sequentially then we must use the format
below for the READ. There is very little difference
between this format and the format of the ordinary sequential READ except that in this format the NEXT RECORD
phrase is used.

Notes This format is used to access a Relative file
sequentially when the ACCESS MODE has been declared
as DYNAMIC and the file has been opened for INPUT or I-O.
For Relative files the next record pointer may be positioned by the
START verb or by doing a direct READ.
The READ NEXT will read the record pointed to by
the next record pointer (This will be the current record if positioned by
the START and the next record if positioned by a
direct READ).
The AT END statement is activated when the end of
the file has been reached
|
The WRITE verb |
The format for writing sequentially to a Relative file is the same as
that used for writing to a Sequential file, but to write directly to a
Relative file a key must be used and this requires a different WRITE format.

Operation To write a record directly to a Relative file
- The record value must be placed in the files record buffer.
- The key value must be placed in the file's relative key.
- The WRITE must be executed.
When the WRITE is executed the data in the record
buffer is written to the record position with a Relative Record Number
equal to the present value of the key.
Notes The INVALID KEY clause must be used
for direct access to Relative files. If the record being written already
'exists' the INVALID KEY clause will be activate and
the statement following the clause will be done. Any other I-O error will
also activate the INVALID KEY clause.
|
The REWRITE verb |
The REWRITE is used to update a record in situ by
overwriting it. The format of the REWRITE verb is
shown below.

Operation The REWRITE is normally used in
the following way;
- The record we wish to update is read directly into the record buffer
(place the key value in the key are and execute the READ).
- The required changes are made to the record in the buffer.
- The record in the buffer is rewritten to the file.
Notes If the file has an ACCESS MODE of
SEQUENTIAL then the INVALID KEY
clause cannot be used but if the ACCESS MODE is RANDOM or DYNAMIC the
INVALID KEY clause must be present (unless declaratives are being
used).
When the file has ACCESS MODE IS SEQUENTIAL the
record that is replaced must have been the subject of a READ
or START before the REWRITE
is used.
For all access modes the file must be opened for I-O.
|
The DELETE verb |

Operation To delete a record
- The Relative Record Number of the record to be deleted must be
placed in the file's relative key.
- Then the DELETE must be executed.
The record with the Relative Record number equal to the current value
of the key are will be deleted.
Notes To use the DELETE, the file must
have been opened for I-O.
When the ACCESS MODE IS SEQUENTIAL a READ statement must access the record to be deleted.
When the ACCESS MODE IS RANDOM or
DYNAMIC the record to be deleted is identified by the file's
Relative key.
If the record does not exist the INVALID KEY
statement will be activated.
Note that when a record is deleted its space does not become available
to other records.
|
The START verb |
In Relative files, the only thing the the START
verb is used for, is to control the position of the next record pointer.
Where the START verb appears in a program it is
usually followed by a sequential READ or WRITE.

Operation To position the Next Record Pointer at a particular
record
- Move the Relative Record Number of the record to the file's relative
key.
- Execute the START..KEY IS EQUAL TO
To position the Next Record Pointer at the first record in the file
- Move zeros to the file's relative key.
- Execute the START..KEY IS GREATER THAN
To position the pointer at the last record in the file
- Move all 9's to the file's relative key.
- Execute the START..KEY IS LESS THAN
Notes KeyDataName is the file's relative key. It is
the key of comparison.
The file must be opened for INPUT or I-O when the
START is executed.
Execution of the START statement does not change
the contents of the record area (i.e. the START does
not actually read the record it merely positions the next record pointer).
When the START is executed the Next Record Pointer
is set to the first record in the file whose key satisfies the condition.
If no record satisfies the condition then the INVALID
KEY clause is activated. |