OPEN Statement

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

  1. File is the name of a file described in the Data Division. It may not be a sort file.
  2. The I-O phrase can be specified only for files that reside on disk.
  3. The MASS-UPDATE and BULK-ADDITION phrases may be specified only for indexed files and may not be specified along with the INPUT phrase.
  4. The WITH NO REWIND phrase may be specified only for sequential files opened with the INPUT or OUTPUT phrase.
  5. If EXCLUSIVE is specified, then lock-option may not be specified.

General Rules

  1. A successful OPEN statement prepares the file for further processing by other I/O statements and puts the file in its open mode.
  2. 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.
  3. 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.
  4. 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
  5. 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  
  6. 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.)
  7. 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.
  8. 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.
  9. 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.
  10. An OPEN statement with multiple files is equivalent to a series of OPEN statements referencing the files in the order named.
  11. 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.
  12. 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.
  13. The following phrases imply the ALLOWING ALL form of file locking:
    1. No lock-option specified
    2. ALLOWING ALL
    3. ALLOWING WRITERS
    4. 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.

  14. The following phrases imply the ALLOWING READERS form of file locking:
    1. ALLOWING READERS
    2. 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.

  15. These phrases imply the ALLOWING NO OTHERS form of file locking:
    1. ALLOWING NO OTHERS
    2. EXCLUSIVE or WITH LOCK specified with the OUTPUT, I-O, or EXTEND phrases
    3. WITH MASS-UPDATE
    4. 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.

  16. 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.
  17. 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.
  18. 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.
  19. 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.
  20. Note that the IBM DOS/VS COBOL -Cv compatibility mode supports Reversed File Reads.