HP 3000 Manuals

OPEN Statement [ HP COBOL II/XL Reference Manual ] MPE/iX 5.0 Documentation


HP COBOL II/XL Reference Manual

OPEN Statement 

The OPEN statement opens a specified file or files.  It also performs
checking and writing of labels, and other input or output operations.

Syntax 

[]
Where file-name-1 through file-name-4 are the files to be opened. Description The NO REWIND phrase can only be used for sequential files. It has no meaning for indexed, random, or relative files and must not be used for such files. When using ANSI74 entry point, EXTEND can only be used for sequential files. The REVERSED phrase is not implemented in HP COBOL II. If used, it is treated as a comment. The REVERSED phrase is an obsolete feature of the 1985 ANSI COBOL standard. You can use a single OPEN statement to open several files. The files to be opened need not have the same organization or access. However, each file must have a description equivalent to the description used for it when it was created. Prior to the successful execution of an OPEN statement for a file, the file must not be referenced implicitly or explicitly by the execution of a statement. When a file has been successfully opened, its associated record area is made available to the program. However, execution of an OPEN statement does not obtain or release any data record of the opened file. The various input-output statements must be used to do this. The INPUT, OUTPUT, I-O, or EXTEND phrases must only be used once in an OPEN statement for any given file. You may open a file with any of the phrases in the same program. However, the file must be closed each time using a CLOSE statement without the LOCK phrase (without the REEL/UNIT phrase in the case of sequential files) before another OPEN statement can be issued for that file. The INPUT phrase opens a file for input operations. If the OPTIONAL phrase is specified in the SELECT clause of a file and the file is not present, the first READ statement for the file causes an AT END condition. The OUTPUT phrase creates a file if it does not already exist and opens it for output operations. When the output file is opened, it contains no data records. The file created is a job or session temporary file using the formal file designator specified in the SELECT clause.[REV BEG] For information about how INDEX files are created, see Appendix H , "MPE XL System Dependencies."[REV END] The I-O phrase permits the opening of a file for both input and output operations. As an HP extension to ANSI COBOL'74, HP COBOL II creates the file if it does not already exist. Under ANSI COBOL'85, the file is not created unless the OPTIONAL keyword is specified in the SELECT clause. This also applies to the EXTEND phrase. When files are opened with the INPUT or I-O phrase, the OPEN statement (without the EXTEND or NO REWIND phrases in the case of sequential files) sets the file position indicator to the first record currently existing within the file. (Indexed files use the prime record key to determine the first record to be accessed.) If no records exist in the file, the file position indicator is set in such a manner that the next executed format 1 READ statement for the file results in an AT END condition. For a relative or indexed file in the dynamic access mode, execution of an OPEN I-O statement followed by one or more WRITE statements and then a READ NEXT statement causes the READ NEXT statement to access the first record in the file at the time the READ NEXT statement executes. This is an incompatible feature with ANSI COBOL'74. [REV BEG] In ANSI COBOL'74, the above sequence of events causes the READ NEXT statement to access the record in the file that was first when the OPEN statement executed, not necessarily the record that is first when the READ NEXT executes. If one of the WRITE statements inserts a record with a key or relative record number lower than any other record in the file, that record would not be read by the READ NEXT. This is the difference between ANSI COBOL'74 and ANSI COBOL'85.[REV END] For compatibility with ANSI COBOL'74, use the ANSI74 entry point. If a CLOSE statement has not been issued for an open file when a STOP RUN statement (or a GOBACK statement in a main program) is executed, the file is automatically closed by the COBOL run-time system. Label Records The LABEL RECORDS clause of the file description entry for a file indicates whether label records are present in the file. The following rules apply when label records are present: 1. When the INPUT phrase is used in the OPEN statement, standard labels are checked in accordance with the conventions for input label checking. Any user labels specified for the file are processed according to the procedure specified by a format 2 USE statement. 2. When the OUTPUT phrase is used in the OPEN statement, standard labels are written in accordance with the conventions for output label writing. Any user labels specified for the file are written according to the procedure specified by a format 2 USE statement. 3. When the I-O phrase is used in an OPEN statement, standard labels are checked in accordance with the conventions for input-output label checking. New standard labels are written in accordance with input-output label writing. Any user labels specified for the file are processed according to the procedure specified by a format 2 USE statement. When label records are specified, but are not present, and the file was opened using the INPUT phrase, an input-output routine error results. When label records are present, but not specified, and the file was opened using the INPUT phrase, the label records are ignored. EXTEND, REVERSE, and NO REWIND Phrases The REVERSE and NO REWIND phrases apply to sequential files. When using the ANSI74 entry point, EXTEND can only be used for sequential files. The REVERSE and NO REWIND phrases are not recognized by the HP COBOL II compiler, and are treated as comments if specified. The EXTEND phrase, when specified in an OPEN statement, positions the file immediately following the last logical record of the file. Subsequent WRITE statements for the file add records at the end of the file as though the file had been opened with the OUTPUT phrase. If you specify the EXTEND phrase for multiple file reels, a compilation error diagnostic appears. This is done to conform to the 1974 ANSI COBOL standard. The object program on an HP computer system allows the EXTEND operation to execute, even for multiple file reels. However, any files following the referenced file are written over and are made inaccessible. When the EXTEND phrase is specified and the LABEL RECORDS clause of the file description for the file indicates the existence of label records, the execution of the OPEN statement includes the following steps: 1. The beginning file labels are processed only if the file resides on a single reel or unit. Any user labels specified for the file are processed according to the procedure specified by a format 2 USE statement. 2. Processing then proceeds as though the file had been opened with the OUTPUT phrase. Permissible Statements The following tables indicate the statements permitted to be executed for a file of a given organization opened in a given open mode. Table 9-3. Sequential Organization ----------------------------------------------------------------------------------------------------- | | | | | Open Mode | | Statement | | | | | ----------------------------------------------------------------------------------------------------- | | | | | | | | Input | Output | Input-Output | Extend | | | | | | | ----------------------------------------------------------------------------------------------------- | | | | | | | Read | X | | X | | | | | | | | ----------------------------------------------------------------------------------------------------- | | | | | | | Write | | X | X | X | | | | | | | ----------------------------------------------------------------------------------------------------- | | | | | | | Rewrite | | | X | | | | | | | | ----------------------------------------------------------------------------------------------------- WRITE with Input-Output mode is an HP extension to the ANSI COBOL standard. Table 9-4. Relative and Indexed Organization ----------------------------------------------------------------------------------------------------- | | | | | File Access | | Open Mode | | Method | Statement | | | | | | ----------------------------------------------------------------------------------------------------- | | | | | | | | | Input | Output | Input-Output | | | | | | | ----------------------------------------------------------------------------------------------------- | | | | | | | Sequential | READ | X | | X | | | | | | | ----------------------------------------------------------------------------------------------------- | | | | | | | | WRITE | | X | | | | | | | | ----------------------------------------------------------------------------------------------------- | | | | | | | | REWRITE | | | X | | | | | | | ----------------------------------------------------------------------------------------------------- | | | | | | | | START | X | | X | | | | | | | ----------------------------------------------------------------------------------------------------- | | | | | | | | DELETE | | | X | | | | | | | ----------------------------------------------------------------------------------------------------- | | | | | | | | SEEK (Rel. only) | [REV BEG] | | [REV END] | | | | | | | ----------------------------------------------------------------------------------------------------- | | | | | | | Random | READ | X | | X | | | | | | | ----------------------------------------------------------------------------------------------------- | | | | | | | | WRITE | | X | X | | | | | | | ----------------------------------------------------------------------------------------------------- | | | | | | | | REWRITE | | | X | | | | | | | ----------------------------------------------------------------------------------------------------- | | | | | | | | START | | | | | | | | | | ----------------------------------------------------------------------------------------------------- | | | | | | | | DELETE | | | X | | | | | | | ----------------------------------------------------------------------------------------------------- | | | | | | | | SEEK (Rel. only) | X | | X | | | | | | | ----------------------------------------------------------------------------------------------------- | | | | | | | Dynamic | READ | X | | X | | | | | | | ----------------------------------------------------------------------------------------------------- | | | | | | | | WRITE | | X | X | | | | | | | ----------------------------------------------------------------------------------------------------- | | | | | | | | REWRITE | | | X | | | | | | | ----------------------------------------------------------------------------------------------------- | | | | | | | | START | X | | X | | | | | | | ----------------------------------------------------------------------------------------------------- | | | | | | | | DELETE | | | X | | | | | | | ----------------------------------------------------------------------------------------------------- | | | | | | | | SEEK (Rel. only) | X | | X | | | | | | | -----------------------------------------------------------------------------------------------------
NOTE For ANSI, EXTEND can be used with the WRITE statement only if Access Mode Sequential.
The SEEK statement is an HP extension to the ANSI COBOL standard. Table 9-5. Random Organization ------------------------------------------------------------------------------------------------- | | | | | Open Mode | | Statement | | | | | ------------------------------------------------------------------------------------------------- | | | | | | | Input | Output | Input-Output | | | | | | ------------------------------------------------------------------------------------------------- | | | | | | Seek | X | | X | | | | | | ------------------------------------------------------------------------------------------------- | | | | | | Read | X | | X | | | | | | ------------------------------------------------------------------------------------------------- | | | | | | Rewrite | | | X | | | | | | ------------------------------------------------------------------------------------------------- | | | | | | Write | | X | X | | | | | | ------------------------------------------------------------------------------------------------- FILE STATUS Data Item If the file named in the OPEN statement has a FILE STATUS data item associated with it, the FILE STATUS data item is updated following the execution of the OPEN statement to indicate whether or not the attempt to open the file was successful. Refer to "FILE STATUS Clause" in Chapter 6 for valid combinations of status keys 1 and 2.[REV BEG] For more information on handling I/O errors, see "Input-Output Error Handling Procedures" .[REV END]


MPE/iX 5.0 Documentation