Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

Open modes and types of processing (indexed files)

The language elements available for use in a COBOL program allow indexed 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 indexed files.

  • ACCESS MODE IS SEQUENTIAL

    enables the sequential creation of an indexed file. The records must be sorted in ascending order of their record keys before they are made available to the WRITE statement.
    Before each WRITE statement, the RECORD KEY data item must therefore be supplied with the record key of the record to be output. Each new key must be greater than the preceding one. If this is not the case, an INVALID KEY condition exists, 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.

  • ACCESS MODE IS DYNAMIC or RANDOM

    enables the random creation of an indexed file. Note that file creation based on ascending record keys is performed more efficiently.

OPEN EXTEND

OPEN EXTEND enables an existing file to be extended. The ACCESS MODE clause is used in the same way as for OPEN OUTPUT.

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...
[KEY IS...]
[INVALID KEY...]
[NOT INVALID KEY...]
[END-START]

Statement not permitted

START...
[KEY IS...]
[INVALID KEY...]
[NOT INVALID KEY]
[END-START]

READ

READ...[NEXT | PREVIOUS]
[INTO...]
[AT END...]
[NOT AT END...]
[END-READ...]

READ...
[INTO...]
[KEY IS...]
[INVALID KEY...]
[NOT INVALID KEY...]
[END-READ]

For sequential access:

READ...{NEXT | PREVIOUS}
[INTO...]
[AT END...]
[NOT AT END...]
[END-READ]

For random access
READ...
[INTO...]
[KEY IS...]
[INVALID KEY...]
[NOT INVALID KEY...]
[END-READ]

Table 32: Permitted I-O statements for OPEN INPUT


Indexed 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 record keys.
    The (ALTERNATE) KEY item is not evaluated by READ. However, 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 determine the record key of the first record to be read and thus establishes the starting point for subsequent sequential read operations (see also section "I-O status"). If the relation condition cannot be satisfied by any record key of the file, an INVALID KEY condition exists, and START makes a branch to the INVALID KEY statement or to the declared USE procedure.

  • ACCESS MODE IS RANDOM

    enables records of the file to be randomly read. READ retrieves the records in a user-specified order; access to each record is effected via its record key.
    Accordingly, the record key of each record to be read must be supplied in the (ALTERNATE) KEY data item prior to each READ operation.
    If the record key of an unavailable record is specified, an INVALID KEY condition exists, and READ branches to the INVALID KEY statement or to the USE procedure declared for this condition.

  • ACCESS MODE IS DYNAMIC

    permits random and/or sequential reading of the file. The desired access mode is selected via the format of the READ statement (see table 33).
    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.

Records in an indexed file opened in OPEN I-O mode can be

  • read,

  • added,

  • updated by the program, and

  • overwritten or

  • deleted.

The following table lists the options available for OPEN I-O

Statement

Entry in the ACCESS MODE clause

SEQUENTIAL

RANDOM

DYNAMIC

START

START...
[KEY IS...]
[INVALID KEY...]
[NOT INVALID KEY...]
[END-START]

Statement not permitted

START...
[KEY IS...]
[INVALID KEY...]
[NOT INVALID KEY]
[END-START]

READ

READ...[NEXT | PREVIOUS]
[INTO...]
[AT END...]
[NOT AT END...]
[END-READ...]

READ...
[INTO...]
[KEY IS...]
[INVALID KEY...]
[NOT INVALID KEY...]
[END-READ]

For sequential access:

READ...{NEXT | PREVIOUS}
[INTO...]
[AT END...]
[NOT AT END...]
[END-READ]

For random access:
READ...
[INTO...]
[KEY IS...]
[INVALID KEY...]
[NOT INVALID KEY...]
[END-READ]

REWRITE

REWRITE...
[FROM...]
[INVALID KEY...]
[NOT INVALID KEY...]
[END-REWRITE]

REWRITE...
[FROM...]
[INVALID KEY...]
[NOT INVALID KEY...]
[END-REWRITE]

REWRITE...
[FROM...]
[INVALID KEY...]
[NOT INVALID KEY...]
[END-REWRITE]

WRITE

Statement not permitted

WRITE...
[FROM...]
[INVALID KEY...]
[NOT INVALID KEY...]
[END-WRITE]

WRITE...
[FROM...]
[INVALID KEY...]
[NOT INVALID KEY...]
[END-WRITE]

DELETE

DELETE...
[END-DELETE]

DELETE...
[INVALID KEY...]
[NOT INVALID KEY...]
[END-DELETE]

DELETE...
[INVALID KEY...]
[NOT INVALID KEY...]
[END-DELETE]

Table 33: Permitted I/O statements for OPEN I-O


OPEN I-O assumes that the file to be processed already exists. It is therefore not possible to create a new indexed 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 rewritten with REWRITE, or

    • logically deleted with DELETE.

    However, it must be noted that

    • no further I-O statement must be executed for this file, and

    • the RECORD KEY data item must not be changed

    between the READ statement and the REWRITE or DELETE statement.

  • 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 RECORD KEY data item must be supplied with the key of the record that is to be added, rewritten, or deleted. If the following conditions apply the number of an already existing record is specified for WRITE, or the number of an unavailable 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.