OPEN statement

The OPEN statement initiates the processing of files. It also checks or writes labels, or both.

Read syntax diagramSkip visual syntax diagram
Format 1: OPEN statement for sequential files

>>-OPEN--------------------------------------------------------->

   .---------------------------------------------------------.   
   |          .------------------------------------------.   |   
   V          V                                          |   |   
>----+-INPUT----file-name-1--+-------------------------+-+-+-+-><
     |                       |          (1)            |   |     
     |                       +-REVERSED----------------+   |     
     |                       |                     (1) |   |     
     |                       '-+------+--NO REWIND-----'   |     
     |                         '-WITH-'                    |     
     |         .--------------------------------------.    |     
     |         V                                      |    |     
     +-OUTPUT----file-name-2--+---------------------+-+----+     
     |                        '-+------+--NO REWIND-'      |     
     |                          '-WITH-'                   |     
     |      .-------------.                                |     
     |      V             |                                |     
     +-I-O----file-name-3-+--------------------------------+     
     |         .-------------.                             |     
     |         V             |                             |     
     '-EXTEND----file-name-4-+-----------------------------'     

Notes:
  1. The REVERSED and WITH NO REWIND phrases are syntax checked, but have no effect on the execution of the program.
Read syntax diagramSkip visual syntax diagram
Format 2: OPEN statement for indexed and relative files

         .-----------------------------.   
         |          .-------------.    |   
         V          V             |    |   
>>-OPEN----+-INPUT----file-name-1-+--+-+-----------------------><
           |         .-------------. |     
           |         V             | |     
           +-OUTPUT----file-name-2-+-+     
           |      .-------------.    |     
           |      V             |    |     
           +-I-O----file-name-3-+----+     
           |         .-------------. |     
           |         V             | |     
           '-EXTEND----file-name-4-+-'     

Read syntax diagramSkip visual syntax diagram
Format 3: OPEN statement for line-sequential files

         .-----------------------------.   
         |          .-------------.    |   
         V          V             |    |   
>>-OPEN----+-INPUT----file-name-1-+--+-+-----------------------><
           |         .-------------. |     
           |         V             | |     
           +-OUTPUT----file-name-2-+-+     
           |         .-------------. |     
           |         V             | |     
           '-EXTEND----file-name-4-+-'     

The phrases INPUT, OUTPUT, I-O, and EXTEND specify the mode to be used for opening the file. At least one of the phrases INPUT, OUTPUT, I-O, or EXTEND must be specified with the OPEN keyword. The INPUT, OUTPUT, I-O, and EXTEND phrases can appear in any order.

INPUT
Permits input operations.
OUTPUT
Permits output operations. This phrase can be specified when the file is being created.

Do not specify OUTPUT for files that contain records. The file will be replaced by new data.

If the OUTPUT phrase is specified for a file that already contains records and either the SdU or SFS file system is used, the file will be deleted and recreated.

I-O
Permits both input and output operations. The I-O phrase can be specified only for files assigned to direct access devices.

The I-O phrase is not valid for line-sequential files.

EXTEND
Permits output operations that append to or create a file.

The EXTEND phrase is allowed for sequential access files only if the new data is written in ascending sequence. The EXTEND phrase is allowed for files that specify the LINAGE clause.

file-name-1, file-name-2, file-name-3, file-name-4
Designate a file upon which the OPEN statement is to operate. If more than one file is specified, the files need not have the same organization or access mode. Each file-name must be defined in an FD entry in the DATA DIVISION and must not name a sort or merge file. The FD entry must be equivalent to the information supplied when the file was defined.
REVERSED
The REVERSED phrase is syntax checked, but has no effect on the execution of the program.
NO REWIND
The NO REWIND phrase is syntax checked, but has no effect on the execution of the program.

For information on file sizes, see Compiler limits.