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:
- The
OPENstatement has 4 modes,INPUT,OUTPUT,I-O, and Extend. - The
OPENstatement updates theFILE STATUSvariable. - An attempt to
OPENa file that is alreadyOPENfails with a file status of 41. - Failures of the
OPENstatement may be trapped inDECLARATIVES. - A file must have performed a successful
OPENstatement before it may execute a successfulCLOSE,DELETE,READ,START, orREWRITE,UNLOCK, orWRITEstatement. - A file that has successfully executed an
OPEN INPUT, orOPEN I-Ostatement positions the file pointer at the first logical record. - The default scope of the
OPENstate of a file is the current program. If the file is described asGLOBAL, the scope of theOPENstate of the file extends to the entire compilation unit, including nested programs. If the file is described asEXTERNAL, the scope of theOPENstate extends to separately compiled programs that are also described asEXTERNAL. - The same file can be
OPEN'ed andCLOSE'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 theSELECTphrase, or in theOPENstatement. File locking considerations are described below. - The
SHARINGclause in theSELECTstatement sets a level of restrictiveness on whether a file that has beenOPEN'ed can beOPEN'ed by another file connector. Conditions causing a failedOPENare marked with the word “Fails”, and conditions leading to a successfulOPENare marked with the word “Succeeds” in the table below:
- A
SHARINGphrase on anOPENstatement takes precedence over aSHARINGphrase in aSELECTstatement. - The
WITH NO REWINDphrase is treated as commentary. - The
WITH LOCKphrase is treated as commentary.
General Rules: OPEN INPUT
- A file that is
OPEN INPUTmay successfully execute theSTART,READ,UNLOCK, andCLOSEstatements. - A file that is
OPEN INPUTmay not make updates to the file via theDELETE,REWRITE, orWRITEstatement. - A file that has successfully executed an
OPEN INPUTstatement positions the file pointer at the first logical record. - If the file does not exist when the
OPEN INPUTstatement is executed, then theOPENstatement fails with aFILE STATUSof 35 unless theSELECTphrase of the file contains theOPTIONALphrase. In this case, theOPENisSUCCESSFUL, but the firstREADfails with anend-of-filestatus code.
General Rules: OPEN INPUT REVERSED
OPEN INPUT REVERSEis supported forRECORD SEQUENTIALfiles with fixed length records.- After performing an
OPEN INPUT REVERSE, theREADstatements 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:AAAAAAAAAABBBBBBBBBBCCCCCCCCCC
The phrase:open input log-file reversed.read log-file into ws-log-record.
Returns the last record, with valuesCCCCCCCCCC.
General Rules: OPEN OUTPUT
- A file that is
OPEN OUTPUTcreates a new file in the current directory or in the file designated by theCOB_FILE_PATHenvironment variable. - A file that is
OPEN OUTPUTmay successfully execute theUNLOCK,WRITE, andCLOSEstatements. - A file that is
OPEN OUTPUTmay not performREADstatements, or make updates to the file via the DELETE, orREWRITEstatement. - A file that has successfully executed an
OPEN OUTPUTstatement positions the file pointer at beginning of the newly created file. - If the file does not exist when the
OPEN OUTPUTstatement is executed, then theOPEN OUTPUTstatement creates the file. Note that if the file does exist when theOPEN OUTPUTstatement is executed then the existing file is removed, and a new instance of the file is created.
General Rules: OPEN I-O
- A file that is
OPEN I-Omay successfully execute theREAD,WRITE,REWRITE,START,DELETE,UNLOCK, andCLOSEstatements. - A file that has successfully executed an
OPEN I-Ostatement positions the file pointer at the first logical record. - If the file does not exist when the
OPEN I-Ostatement is executed, then theOPENstatement fails with aFILE STATUSof 35 unless theSELECTphrase of the file contains theOPTIONALphrase. In this case, the file is created, and theOPENisSUCCESSFUL.
General Rules: OPEN EXTEND
- A file that is
OPEN EXTENDmay successfully execute theUNLOCK,WRITE, andCLOSEstatements. - A file that is
OPEN EXTENDmay not performREADstatements, or make updates to the file via theDELETE, orREWRITEstatement. - A file that has successfully executed an
OPEN EXTENDstatement positions the file pointer after the last record. - If the file does not exist when the
OPENExtend statement is executed, then theOPENstatement fails with aFILE STATUSof 35 unless theSELECTphrase of the file contains theOPTIONALphrase. In this case, the file is created, and theOPENisSUCCESSFUL.
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.