Rational Developer for System z
COBOL for Windows, Version 7.5, Language Reference


OPEN statement

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

Format 1: sequential files
Read syntax diagramSkip visual syntax diagram>>-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..

Format 2: indexed and relative files
Read syntax diagramSkip visual syntax diagram         .-----------------------------.  
         |          .-------------.    |  
         V          V             |    |  
>>-OPEN----+-INPUT----file-name-1-+--+-+-----------------------><
           |         .-------------. |    
           |         V             | |    
           +-OUTPUT----file-name-2-+-+    
           |      .-------------.    |    
           |      V             |    |    
           +-I-O----file-name-3-+----+    
           |         .-------------. |    
           |         V             | |    
           '-EXTEND----file-name-4-+-'    
 

Format 3: line-sequential files
Read syntax diagramSkip visual syntax diagram         .-----------------------------.  
         |          .-------------.    |  
         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.

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.

General rules

Label records

Label processing is not supported. A warning message is issued if any of the following language elements are encountered:

 

OPEN statement notes

  1. The successful execution of an OPEN statement determines the availability of the file and results in that file being in open mode. A file is available if it is physically present and is recognized by the input-output control system. The following table shows the results of opening available and unavailable files. For more information regarding file availability, see the COBOL for Windows Programming Guide.
    Table 48. Availability of a file
    Opened as File is available File is unavailable
    INPUT Normal open Open is unsuccessful.
    INPUT (optional file) Normal open Normal open; the first read causes the at end condition or the invalid key condition.
    I-O Normal open Open is unsuccessful.
    I-O (optional file) Normal open Open causes the file to be created.
    OUTPUT Normal open; the file contains no records Open causes the file to be created.
    EXTEND Normal open Open is unsuccessful.
    EXTEND (optional file) Normal open Open causes the file to be created.
  2. The successful execution of the OPEN statement places the file in open status and makes the associated record area available to the program.
  3. The OPEN statement does not obtain or release the first data record.
  4. You can move data to or from the record area only when the file is in open status.
  5. An OPEN statement must be successfully executed prior to the execution of any of the permissible input-output statements, except a SORT or MERGE statement with the USING or GIVING phrase. In the following table, an 'X' indicates that the specified statement can be used with the open mode given at the top of the column.

Table 49. Permissible statements for sequential files
Statement Input open mode Output open mode I-O open mode Extend open mode
READ X   X  
WRITE   X   X
REWRITE     X  

In the following table, an 'X' indicates that the specified statement, used in the access mode given for that row, can be used with the open mode given at the top of the column.

Table 50. Permissible statements for indexed and relative files
File access mode Statement Input open mode Output open mode I-O open mode Extend open mode
Sequential READ X   X  
WRITE   X   X
REWRITE     X  
START X   X  
DELETE     X  
Random READ X   X  
WRITE   X X  
REWRITE     X  
START        
DELETE     X  
Dynamic READ X   X  
WRITE   X X  
REWRITE     X  
START X   X  
DELETE     X  

In the following table, an 'X' indicates that the specified statement can be used with the open mode given at the top of the column.

Table 51. Permissible statements for line-sequential files
Statement Input open mode Output open mode I-O open mode Extend open mode
READ X      
WRITE   X   X
REWRITE        
  1. A file can be opened for INPUT, OUTPUT, I-O, or EXTEND (sequential and line-sequential files only) in the same program. After the first OPEN statement execution for a given file, each subsequent OPEN statement execution must be preceded by a successful CLOSE file statement execution without the LOCK phrase.
  2. If the FILE STATUS clause is specified in the file-control entry, the associated file status key is updated when the OPEN statement is executed.
  3. If an OPEN statement is issued for a file already in the open status, the EXCEPTION/ERROR procedure (if specified) for this file is executed.

Terms of use | Feedback

Copyright IBM Corporation 1996, 2008.
This information center is powered by Eclipse technology. (http://www.eclipse.org)