The various language elements available for use in a COBOL program allows relative files to be
created,
read,
extended (by adding new records), and
updated (by changing or deleting existing records).
The individual I-O statements that may be used in the program to process a given file are determined by its open mode, which is specified in the OPEN statement:
OPEN OUTPUT
Regardless of the phrase in the ACCESS MODE clause, WRITE is permitted as an I-O statement in the following format:
WRITE...[FROM...] [INVALID KEY...] [NOT INVALID KEY...] [END WRITE...]
In this mode, it is only possible to create (load) new relative files. Depending on the specified access mode, the WRITE statement has the following effect:
ACCESS MODE IS SEQUENTIAL
allows a relative file to be created sequentially i.e. WRITE writes records to the file successively with ascending relative record numbers (starting with 1).
The RELATIVE KEY key item - if specified - is not evaluated by WRITE; it is loaded with the value of the (automatically incremented) relative record number of the last record to be written.ACCESS MODE IS RANDOM or DYNAMIC
(both phrases have the same meaning here) enables the random creation of a file. WRITE causes each record to be written to the position in the file that is indicated by its relative record number.
Thus, before each WRITE statement is executed, the RELATIVE KEY item must be supplied with the relative record number which the record to be written is to receive in the file. If the number of an already existing record is specified, the INVALID KEY condition occurs, and WRITE branches to the INVALID KEY statement or to the declared USE procedure without writing the record. It is thus not possible to overwrite records in this case.
OPEN EXTEND
OPEN EXTEND is used to extend an existing file. Access is only possible in sequential mode.
ACCESS MODE IS SEQUENTIAL
permits a relative file to be extended sequentially. Starting with the highest key+1, WRITE writes the records with continuous, ascending record numbers to the file.The RELATIVE KEY key item, if specified, is not interpreted by WRITE; it contains the (automatically incremented) relative record number of the last written record.
OPEN INPUT
The phrase specified in the ACCESS MODE clause determines which I-O statements or statement formats are permitted. The following table lists the options available for OPEN INPUT:
Statement |
Entry in the ACCESS MODE clause |
||
SEQUENTIAL |
RANDOM |
DYNAMIC |
|
START |
START... |
Statement not permitted |
START... |
READ |
READ...[NEXT | PREVIOUS] |
READ... |
For sequential access: READ...{NEXT | PREVIOUS} |
For random access: |
Table 27: Permitted I-O statements for OPEN INPUT
Relative files can be read in this mode. Depending on the access mode specified, the READ statement has the following effect:
ACCESS MODE IS SEQUENTIAL
permits the file to be read sequentially only. READ retrieves records in ascending (NEXT) or descending (PREVIOUS) order, based on the value of the relative record numbers.
The RELATIVE KEY key data item - if specified - is not evaluated by READ; it contains the relative record number of the last record that was read. However, if a RELATIVE KEY key data item was declared, a START statement can be used to position to any record of the file before execution of a READ statement: START uses a relation condition to establish the relative record number of the first record to be read and thus defines the starting point for sequential read operations that follow.
If the comparison relation condition cannot be satisfied by any relative record number of the file, an INVALID KEY condition exists, and START transfers control to the imperative statement specified in the INVALID KEY phrase or to the declared USE procedure.ACCESS MODE IS RANDOM
enables records of the file to be read randomly. READ retrieves the records in the specified order; access to each record is effected via its relative record number.Accordingly, the relative record number of each record to be read must be supplied in the RELATIVE KEY data item prior to each READ operation. If the number of a unavailable record (e.g. an empty record) is specified, the INVALID KEY condition exists, and READ transfers control to the INVALID KEY statement or to the USE procedure declared for this condition.
ACCESS MODE IS DYNAMIC
permits random and sequential reading of the file. The applicable access mode is selected via the format of the READ statement (see table 27).
In this case, a START statement is meaningful for sequential reading only.
OPEN I-O
The phrase specified in the ACCESS MODE clause determines which I-O statements or statement formats are permitted. The following table lists the options available for OPEN I-O:
Statement |
Entry in the ACCESS MODE clause |
||
SEQUENTIAL |
RANDOM |
DYNAMIC |
|
START |
START... |
Statement not permitted |
START... |
READ |
READ...[NEXT | PREVIOUS] |
READ... |
For sequential access: |
For random access: |
|||
REWRITE |
REWRITE... |
REWRITE... |
REWRITE... |
WRITE |
Statement not allowed |
WRITE... |
WRITE... |
DELETE |
DELETE... |
DELETE... |
DELETE... |
Table 28: Permitted I-O statements for OPEN I-O
When a file is opened in this mode, records can be
read,
added,
updated by the program, and
overwritten or
deleted.
OPEN I-O assumes that the file to be processed already exists. It is therefore not possible to create a new relative file in this mode.
The specified access mode determines the type of processing that can be performed, as well as the effect of the individual I-O statements:
ACCESS MODE IS SEQUENTIAL
As in the case of OPEN INPUT, this access mode permits the sequential reading of a file with READ and the use of a preceding START to position to any record as the starting point.
In addition, the record that is read by a successful READ operation can be updated by the program and then rewritten with REWRITE or logically deleted with DELETE. However, no other I-O statement should be executed for this file between the READ and REWRITE or DELETE statements.
ACCESS MODE IS RANDOM
enables records to be randomly retrieved with READ (as in OPEN INPUT).
In addition, new records can be inserted into the file with WRITE, and existing records in the file can be rewritten or deleted with REWRITE or DELETE (regardless of whether they were read earlier).
Prior to each WRITE, REWRITE, or DELETE statement, the RELATIVE KEY data item must be supplied with the relative number of the record that is to be added, rewritten, or deleted. If the following cases apply the number of an already existing record is specified for WRITE, or the number of a unavailable record (e.g. an empty record) is specified for REWRITE or DELETE, an INVALID KEY condition exists, and WRITE, REWRITE, or DELETE branches to the INVALID KEY statement or to the USE procedure declared for this event.
ACCESS MODE IS DYNAMIC
allows a file to be processed sequentially or randomly. Here, the desired access mode is selected via the format of the READ statement.