OPEN Statement COBOL

The OPEN statement initiates the processing of files.

OPEN Statement

The successful execution of an OPEN statement determines the availability of the file and results in the file being in an open mode. A file is available if it is physically present and recognized by the runtime system. Table shows the results of opening available and unavailable files.

The successful execution of an OPEN statement makes the associated record area available to the program. If the file connector associated with the file-name is an external file connector, there is only one record area associated with the file connector for the run unit.

The files referenced in the OPEN statement need not all have the same organization or access.

The EXTEND phrase may only be specified for files with sequential access.

The REVERSED and NO REWIND phrases may only be specified for files that are sequential organization.

The EXCLUSIVE phrase indicates that the open is to obtain exclusive access to each file referenced in the OPEN statement until the file is closed. The EXCLUSIVE phrase is redundant for any file for which the LOCK MODE IS EXCLUSIVE clause is specified in its file control entry.

The LOCK phrase indicates that the open is to obtain exclusive access to the associated file until the file is closed. The LOCK phrase is redundant if the EXCLUSIVE phrase is specified in the same OPEN statement or if the LOCK MODE IS EXCLUSIVE clause is specified in the file control entry for the file.

The successful execution of the OPEN statement sets the lock mode of the file using the EXCLUSIVE and LOCK phrases of the OPEN statement, the LOCK MODE clause, if specified, in the file control entry for the file, or configurable defaults for each open mode. The section “ File Locking ” provides a general discussion of lock mode. If the file is opened in shared input-output mode, record locking will apply as described in the section “Record Locking”.

Availability of a File

Availability of a File

Prior to the successful execution of an OPEN statement for a given file, no statement can be executed that references that file, either explicitly or implicitly, except that the file may be listed in the USING or GIVING phrases of a SORT or MERGE statement.

An OPEN statement must be successfully executed prior to the execution of any of the permissible input-output statements. In Table a symbol at an intersection indicates that the specified statement, used in the access mode given for that row, may be used with the open mode given at the top of the column.

A file may be opened with the INPUT, OUTPUT, EXTEND, and I–O phrases in the same program. Following the initial execution of an OPEN statement for a file, each subsequent OPEN statement execution for that same file must be preceded by the execution of a CLOSE statement, without the LOCK, REEL or UNIT phrase, for that file.

Permissible Statements

Permissible Statements

The execution of an OPEN statement causes the value of the specified file status data item, if any, associated with the file to be updated.

INPUT Phrase

INPUT Phrase

If a file opened with the INPUT phrase is an optional file that is not present, the OPEN statement sets the file position indicator to indicate that an optional input file is not present.Otherwise:

  • When sequential or relative files are opened with the INPUT phrase, the file position indicator is set to 1.
  • When indexed files are opened with the INPUT phrase, the file position indicator is set to the characters that have the lowest ordinal position in the collating sequence associated with the file, and the prime record key is established as the key of reference.

The REVERSED and NO REWIND phrases may only be specified if file-name-1 refers to a sequential organization file. Since the NO REWIND phrase is common to the INPUT and OUTPUT phrases, it is discussed separately.

When the REVERSED phrase is specified, the file is positioned at its end by execution of the OPEN statement. Subsequent READ statements for the file make the data records of the file available in reverse order; that is, starting with the last record.

The REVERSED phrase is applicable only to files whose storage medium is capable of reverse motion. It is ignored when not applicable to the storage medium of the file. The RM/COBOL User’s Guide contains specific information regarding support for the REVERSED phrase.

OUTPUT Phrase

OUTPUT {file-name-2 [ WITH LOCK ] [ WITH NO REWIND ]}

Upon successful execution of an OPEN statement with the OUTPUT phrase specified, a file is created. At that time, the associated file contains no data records.

The NO REWIND phrase may only be specified if file-name-2 refers to a sequential organization file.Since the NO REWIND phrase is common to the INPUT and OUTPUT phrases.

I-O Phrase

I-O { file-name-3 [WITH LOCK ]}

The I–O phrase permits the opening of a mass storage file for both input and output operations. If the referenced file does not exist and the OPTIONAL phrase is specified in the SELECT clause for the referenced file, the file is created as a new empty file as is done when the OUTPUT phrase is used.

The I–O phrase can be used only for mass storage files (files assigned to the DISC, DISK, or RANDOM device-type).

When the I–O phrase is specified and the LABEL RECORDS clause indicates that label records are present, the execution of the OPEN statement includes the following:

  • he labels are checked.
  • New labels are written.

When sequential or relative files are opened with the I–O phrase, the file position indicator is set to 1.

When indexed files are opened with the I–O phrase, the file position indicator is set to the characters that have the lowest ordinal position in the collating sequence associated with the file, and the prime record key is established as the key of reference.

In 1985 mode, if the run unit does not have write access to the file, the execution of an OPEN statement with the I–O phrase is unsuccessful and the I–O status value is set to indicate this condition. In 1974 mode, if the run unit does not have write access to the file, an OPEN statement with the I–O phrase is successful; however, any attempt to execute a DELETE, REWRITE, or WRITE statement while in this mode will be unsuccessful.

EXTEND Phrase

EXTEND { file-name-4 [ WITH LOCK ]}

When the EXTEND phrase is specified, the OPEN statement positions the file immediately following the last logical record of that file. Subsequent WRITE statements referencing the file will add records to the file as though the file has been opened with the OUTPUT phrase.

The EXTEND phrase may be specified only if file-name-4 refers to a file with sequential access. The EXTEND phrase must not be specified for a file whose device-type is INPUT.

The last record for a sequential file is the last record written in the file.

The last record for a relative file is the currently existing record with the highest relative record number.

The last record for an indexed file is the currently existing record with the highest prime key value according to the collating sequence of the file. If the indexed file is described with the DUPLICATES phrase in the RECORD KEY clause of its file control entry and the highest prime key value is duplicated within the records of the file, then the last record is the currently existing record with the highest prime key value that was chronologically last released to the file.

NO REWIND Phrase

WITH NOREWIND

The NO REWIND phrase can be used only with sequential single reel or unit files. The phrase is ignored if it does not apply to the storage medium on which the file resides.

If the storage medium for the file permits rewinding, the following rules apply:

  1. When the REVERSED, EXTEND or NO REWIND phrase is not specified, execution of the OPEN statement causes the file to be positioned at its beginning.
  2. When the NO REWIND phrase is specified, execution of the OPEN statement does not cause the file to be repositioned; the file must be already positioned at its beginning prior to the execution of the OPEN statement.

OPEN Statement Examples



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

COBOL Topics