The OPEN statement initiates processing of a file.
General Format
OPEN [EXCLUSIVE]
{ {INPUT } { file [WITH NO REWIND] } ... } ...
{OUTPUT} [lock-option ]
{I-O }
{EXTEND}
lock-option may be either of these formats:
{ ALLOWING {NO OTHERS} }
{READERS }
{WRITERS }
{UPDATERS }
{ALL }
{ {WITH} {LOCK } }
{FOR } {MASS-UPDATE }
{BULK-ADDITION}
Syntax Rules
- File is the name of a file described in the Data Division. It may not be a sort file.
- The I-O phrase can be specified only for files that reside on disk.
- The MASS-UPDATE and BULK-ADDITION phrases may be specified only for indexed files and may not be specified along with the
INPUT phrase.
- The WITH NO REWIND phrase may be specified only for sequential files opened with the INPUT or OUTPUT phrase.
- If EXCLUSIVE is specified, then
lock-option may not be specified.
General Rules
- A successful OPEN statement prepares the file for further processing by other I/O statements and puts the file in its open
mode.
- An OPEN may not be performed on a file that is already open. A file is not open when the program is in its initial state or
when the last I/O statement on the file was a successful CLOSE statement.
- Except for the OUTPUT option, and as noted in rule 19 below, a file's organization and data description must match those used
when the file was created.
- The following table indicates the effects of the various OPEN types on files that are and are not available. A file is not
available if it does not exist on the host computer. Note that different results occur for missing files if the file's SELECT
contains the OPTIONAL phrase.
Mode
|
Available
|
Unavailable
|
INPUT
|
Open succeeds
|
Open fails
|
- Optional
|
Open succeeds
|
Open succeeds
|
I-O
|
Open succeeds
|
Open fails
|
- Optional
|
Open succeeds
|
Open creates file
|
OUTPUT
|
Open recreates file
|
Open creates file
|
EXTEND
|
Open succeeds
|
Open fails
|
- Optional
|
Open succeeds
|
Open creates file
|
- The different OPEN types allow for different I/O statements to be used. This is summarized in the following chart.
Statement
|
Input
|
Output
|
I-O
|
Extend
|
READ
|
X
|
|
X
|
|
WRITE
|
|
X
|
X
|
X
|
REWRITE
|
|
|
X
|
|
START
|
X
|
|
X
|
|
DELETE
|
|
|
X
|
|
UNLOCK
|
X
|
|
X
|
|
- After a successful OPEN statement, the file is positioned at its first logical record. (The only exception to this rule is
that a sequential access file opened with the EXTEND phrase is positioned after its last record. Records written to such a
file are appended to the file.)
- A file opened with the OUTPUT phrase is logically recreated. This is equivalent to physically removing the file (if it exists)
and creating it anew.
- If a file is not available, the OPTIONAL phrase is specified in the file's SELECT and the file is opened with the INPUT phrase,
then the first READ on the file will return an end-of-file status condition.
- The execution of the OPEN statement causes the I-O status associated with file to be updated. Note that opening an OPTIONAL
file when that file does not exist will return a different status code than opening it if it does exist.
- An OPEN statement with multiple files is equivalent to a series of OPEN statements referencing the files in the order named.
- The open state of a file is not affected by other instances of the same file in other run units or other programs of the same
run unit. These other files may be opened or closed without affecting the ability of the current program to open the same
file, except as modified by the file locking rules below.
- The EXCLUSIVE, WITH, and ALLOWING phrases allow for various forms of file locking. There are three forms of file locking:
ALLOWING ALL, ALLOWING READERS, and ALLOWING NO OTHERS. File locking is enforced for all file types residing on disk but may
not be enforced for non-disk files for some operating systems.
Note:
The
-Fn compiler option specifies file locking as the default behavior for files that do not have locking or sharing already specified
or implied from within the program. See
File Options for details on this and other options related to file locking.
- The following phrases imply the ALLOWING ALL form of file locking:
- No lock-option specified
- ALLOWING ALL
- ALLOWING WRITERS
- ALLOWING UPDATERS
This file locking mode indicates that other programs can access the file without restriction except that another program may
not execute an OPEN OUTPUT while this program keeps the file open.
- The following phrases imply the ALLOWING READERS form of file locking:
- ALLOWING READERS
- EXCLUSIVE or WITH LOCK specified with the INPUT phrase
A file open in this mode does not allow any other program to open this file other than with the INPUT phrase. Furthermore,
this OPEN will fail if any other programs currently have the file open unless the INPUT phrase was used by all of these other
programs.
- These phrases imply the ALLOWING NO OTHERS form of file locking:
- ALLOWING NO OTHERS
- EXCLUSIVE or WITH LOCK specified with the OUTPUT, I-O, or EXTEND phrases
- WITH MASS-UPDATE
- BULK-ADDITION
This form of file locking does not allow any other programs to open the file, and this OPEN will fail if any other programs
currently have the file open.
- The WITH MASS-UPDATE phrase is equivalent to the WITH LOCK phrase with some additional effects. It may be specified only for
indexed files. This phrase indicates to the runtime system that the file in question will be heavily updated by this program.
The runtime system may be able to use this information to access the file more efficiently. See
Indexed File Considerations for more information about the effects of this phrase and advice on when it should be specified.
- The BULK-ADDITION phrase is equivalent to the MASS-UPDATE phrase with some additional effects. For Vision files, the BULK-ADDITION
phrase opens the file in
bulk addition mode, which substantially increases efficiency when you are writing a large number of new records to the file. BULK-ADDITION
has several significant effects, including some changes to standard COBOL file handling rules. For host file systems other
than Vision, specifying BULK-ADDITION has the same effect as specifying MASS-UPDATE. In this case, none of the special handling
dictated by BULK-ADDITION applies. See Bulk Addition Mode for Vision for more information.
- A file with a LOCK MODE IS EXCLUSIVE phrase in its SELECT treats all OPEN statements as if they were written with the EXCLUSIVE
option.
- The configuration variable EXTRA_KEYS_OK allows you to open an indexed file without specifying all of that file's alternate
keys. For more information, see the listing for EXTRA_KEYS_OK.
- Note that the IBM DOS/VS COBOL
-Cv compatibility mode supports Reversed File Reads.