Skip to content

OPEN Statement

The OPEN statement places a file in a state where it may be processed.

General Format:

       OPEN {INPUT } [optional] [share-opt][REVERSED] {file-1 [WITH NO REWIND]...}... 
            {OUTPUT}                                          [WITH LOCK       ] 
            {I-O   } 
            {Extend}

Where share-opt is:

SHARING WITH READ ONLY or SHARING WITH NO OTHER

Syntax:

file-n is a file described in the File Section with an FD.

General Rules:

  1. The OPEN statement has 4 modes, INPUT, OUTPUT, I-O, and Extend.
  2. The OPEN statement updates the FILE STATUS variable.
  3. An attempt to OPEN a file that is already OPEN fails with a file status of 41.
  4. Failures of the OPEN statement may be trapped in DECLARATIVES.
  5. A file must have performed a successful OPEN statement before it may execute a successful CLOSE, DELETE, READ, START, or REWRITE, UNLOCK, or WRITE statement.
  6. A file that has successfully executed an OPEN INPUT, or OPEN I-O statement positions the file pointer at the first logical record.
  7. The default scope of the OPEN state of a file is the current program. If the file is described as GLOBAL, the scope of the OPEN state of the file extends to the entire compilation unit, including nested programs. If the file is described as EXTERNAL, the scope of the OPEN state extends to separately compiled programs that are also described as EXTERNAL.
  8. The same file can be OPEN'ed and CLOSE'd by different programs in the same run unit, except in cases where the file is operating under restrictive file locking rules, as indicated in the SELECT phrase, or in the OPEN statement. File locking considerations are described below.
  9. The SHARING clause in the SELECT statement sets a level of restrictiveness on whether a file that has been OPEN'ed can be OPEN'ed by another file connector. Conditions causing a failed OPEN are marked with the word “Fails”, and conditions leading to a successful OPEN are marked with the word “Succeeds” in the table below:

    Table 1
  10. A SHARING phrase on an OPEN statement takes precedence over a SHARING phrase in a SELECT statement.
  11. The WITH NO REWIND phrase is treated as commentary.
  12. The WITH LOCK phrase is treated as commentary.

General Rules: OPEN INPUT

  1. A file that is OPEN INPUT may successfully execute the START, READ, UNLOCK, and CLOSE statements.
  2. A file that is OPEN INPUT may not make updates to the file via the DELETE, REWRITE, or WRITE statement.
  3. A file that has successfully executed an OPEN INPUT statement positions the file pointer at the first logical record.
  4. If the file does not exist when the OPEN INPUT statement is executed, then the OPEN statement fails with a FILE STATUS of 35 unless the SELECT phrase of the file contains the OPTIONAL phrase. In this case, the OPEN is SUCCESSFUL, but the first READ fails with an end-of-file status code.

General Rules: OPEN INPUT REVERSED

  1. OPEN INPUT REVERSE is supported for RECORD SEQUENTIAL files with fixed length records.
  2. After performing an OPEN INPUT REVERSE, the READ statements return records in reverse order, beginning with the last record.

    For example in a record sequential file with records of 10 characters, and containing data as follows:

    AAAAAAAAAA
    BBBBBBBBBB
    CCCCCCCCCC

    The phrase:
    open input log-file reversed.
    read log-file into ws-log-record.

    Returns the last record, with values CCCCCCCCCC.

General Rules: OPEN OUTPUT

  1. A file that is OPEN OUTPUT creates a new file in the current directory or in the file designated by the COB_FILE_PATH environment variable.
  2. A file that is OPEN OUTPUT may successfully execute the UNLOCK, WRITE, and CLOSE statements.
  3. A file that is OPEN OUTPUT may not perform READ statements, or make updates to the file via the DELETE, or REWRITE statement.
  4. A file that has successfully executed an OPEN OUTPUT statement positions the file pointer at beginning of the newly created file.
  5. If the file does not exist when the OPEN OUTPUT statement is executed, then the OPEN OUTPUT statement creates the file. Note that if the file does exist when the OPEN OUTPUT statement is executed then the existing file is removed, and a new instance of the file is created.

General Rules: OPEN I-O

  1. A file that is OPEN I-O may successfully execute the READ, WRITE, REWRITE, START, DELETE, UNLOCK, and CLOSE statements.
  2. A file that has successfully executed an OPEN I-O statement positions the file pointer at the first logical record.
  3. If the file does not exist when the OPEN I-O statement is executed, then the OPEN statement fails with a FILE STATUS of 35 unless the SELECT phrase of the file contains the OPTIONAL phrase. In this case, the file is created, and the OPEN is SUCCESSFUL.

General Rules: OPEN EXTEND

  1. A file that is OPEN EXTEND may successfully execute the UNLOCK, WRITE, and CLOSE statements.
  2. A file that is OPEN EXTEND may not perform READ statements, or make updates to the file via the DELETE, or REWRITE statement.
  3. A file that has successfully executed an OPEN EXTEND statement positions the file pointer after the last record.
  4. If the file does not exist when the OPEN Extend statement is executed, then the OPEN statement fails with a FILE STATUS of 35 unless the SELECT phrase of the file contains the OPTIONAL phrase. In this case, the file is created, and the OPEN is SUCCESSFUL.

Code Samples:

       OPEN OUTPUT   SEQ-FILE-1. 
       OPEN INPUT    SEQ-FILE-1. 
       OPEN EXTEND   SEQ-FILE-1. 

       OPEN OUTPUT   REL-FILE-1. 
       OPEN INPUT    REL-FILE-1. 
       OPEN I-O      REL-FILE-1. 

       OPEN OUTPUT   IDX-FILE-1. 
       OPEN INPUT    IDX-FILE-1. 
       OPEN I-O      IDX-FILE-1. 

       OPEN OUTPUT   SHARING WITH NO OTHER IDX-FILE-1. 
       OPEN INPUT    SHARING WITH READ ONLY IDX-FILE-1. 

       OPEN OUTPUT   SEQ-FILE-1 WITH NO REWIND. 
       OPEN INPUT    SEQ-FILE-1 WITH NO REWIND. 

       OPEN INPUT    REL-FILE-1 WITH LOCK. 
       OPEN I-O      REL-FILE-1 WITH LOCK. 

       OPEN INPUT    IDX-FILE-1 WITH LOCK. 
       OPEN I-O      IDX-FILE-1 WITH LOCK.
Back to top