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
OPEN
statement has 4 modes,INPUT
,OUTPUT
,I-O
, and Extend. - The
OPEN
statement updates theFILE STATUS
variable. - An attempt to
OPEN
a file that is alreadyOPEN
fails with a file status of 41. - Failures of the
OPEN
statement may be trapped inDECLARATIVES
. - A file must have performed a successful
OPEN
statement before it may execute a successfulCLOSE
,DELETE
,READ
,START
, orREWRITE
,UNLOCK
, orWRITE
statement. - A file that has successfully executed an
OPEN INPUT
, orOPEN I-O
statement positions the file pointer at the first logical record. - The default scope of the
OPEN
state of a file is the current program. If the file is described asGLOBAL
, the scope of theOPEN
state of the file extends to the entire compilation unit, including nested programs. If the file is described asEXTERNAL
, the scope of theOPEN
state 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 theSELECT
phrase, or in theOPEN
statement. File locking considerations are described below. - The
SHARING
clause in theSELECT
statement sets a level of restrictiveness on whether a file that has beenOPEN
'ed can beOPEN
'ed by another file connector. Conditions causing a failedOPEN
are marked with the word “Fails”, and conditions leading to a successfulOPEN
are marked with the word “Succeeds” in the table below: - A
SHARING
phrase on anOPEN
statement takes precedence over aSHARING
phrase in aSELECT
statement. - The
WITH NO REWIND
phrase is treated as commentary. - The
WITH LOCK
phrase is treated as commentary.
General Rules: OPEN INPUT
- A file that is
OPEN INPUT
may successfully execute theSTART
,READ
,UNLOCK
, andCLOSE
statements. - A file that is
OPEN INPUT
may not make updates to the file via theDELETE
,REWRITE
, orWRITE
statement. - A file that has successfully executed an
OPEN INPUT
statement positions the file pointer at the first logical record. - If the file does not exist when the
OPEN INPUT
statement is executed, then theOPEN
statement fails with aFILE STATUS
of 35 unless theSELECT
phrase of the file contains theOPTIONAL
phrase. In this case, theOPEN
isSUCCESSFUL
, but the firstREAD
fails with anend-of-file
status code.
General Rules: OPEN INPUT REVERSED
OPEN INPUT REVERSE
is supported forRECORD SEQUENTIAL
files with fixed length records.- After performing an
OPEN INPUT REVERSE
, theREAD
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 valuesCCCCCCCCCC
.
General Rules: OPEN OUTPUT
- A file that is
OPEN OUTPUT
creates a new file in the current directory or in the file designated by theCOB_FILE_PATH
environment variable. - A file that is
OPEN OUTPUT
may successfully execute theUNLOCK
,WRITE
, andCLOSE
statements. - A file that is
OPEN OUTPUT
may not performREAD
statements, or make updates to the file via the DELETE, orREWRITE
statement. - A file that has successfully executed an
OPEN OUTPUT
statement positions the file pointer at beginning of the newly created file. - If the file does not exist when the
OPEN OUTPUT
statement is executed, then theOPEN OUTPUT
statement creates the file. Note that if the file does exist when theOPEN OUTPUT
statement 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-O
may successfully execute theREAD
,WRITE
,REWRITE
,START
,DELETE
,UNLOCK
, andCLOSE
statements. - A file that has successfully executed an
OPEN I-O
statement positions the file pointer at the first logical record. - If the file does not exist when the
OPEN I-O
statement is executed, then theOPEN
statement fails with aFILE STATUS
of 35 unless theSELECT
phrase of the file contains theOPTIONAL
phrase. In this case, the file is created, and theOPEN
isSUCCESSFUL
.
General Rules: OPEN EXTEND
- A file that is
OPEN EXTEND
may successfully execute theUNLOCK
,WRITE
, andCLOSE
statements. - A file that is
OPEN EXTEND
may not performREAD
statements, or make updates to the file via theDELETE
, orREWRITE
statement. - A file that has successfully executed an
OPEN EXTEND
statement positions the file pointer after the last record. - If the file does not exist when the
OPEN
Extend statement is executed, then theOPEN
statement fails with aFILE STATUS
of 35 unless theSELECT
phrase of the file contains theOPTIONAL
phrase. In this case, the file is created, and theOPEN
isSUCCESSFUL
.
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.