In this topic, we described about the below sections -
The OPEN statement initiates the processing of files. OPEN statement is to open a file before performing any kind of processing on files like read, write or re-write etc. Programmer can’t able to perform any processing operations without opening a file. OPEN statement also checks or writes labels or both.
Syntax-1: OPEN statement for sequential files
Syntax-2: OPEN statement for indexed and relative files
The INPUT, OUTPUT, I-O and EXTEND phrases can 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 specify in any order.
Allows input (read) operations.
Allows output (write) operations. This phase is specified when the file is newly created and using for records writing. Do not use OUTPUT for files that are -
- Files contain records: The file gets replaced by new data.
- File defined with a DD dummy card: Unpredictable results can occur.
Allows both input and output operations. The I-O phrase can specify only for files assigned to direct access devices.
Allows output operations that append to the existing data in the file or create a file. The EXTEND phrase is allowed for sequential access files only.
If want to append to a file, but are unsure if the file exists, use the SELECT OPTIONAL clause before opening the file in EXTEND mode. The file gets created or appended to depending on whether the file exists.
file-name-1 , file-name-2 , file-name-3 , file-name-4
Specifies a file that is going to open using OPEN statement. More than one file can specify with OPEN statement. 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 but not name a sort or merge file.
File Positioning -
If a file opened with the INPUT phrase is an optional file and not available, the OPEN statement sets the file position indicator to indicate that the file is not available. Execution of an OPEN INPUT or OPEN I-O statement sets the file position indicator -
- For indexed files, to the characters with the lowest ordinal position in the collating sequence.
- For sequential and relative files, to 1.
When the EXTEND phrase is specified, the OPEN statement positions the file immediately after the last record written in the file.
- The successful execution of the OPEN statement places the file in open status and makes the associated record area available to the program.
- The OPEN statement does not obtain or release the first data record.
- An OPEN statement must be successfully executed prior to the execution of any permissible input-output statements.
File Operations Allowed based on Modes -
Below table specifies the list of input-output statements allowed based on the file OPEN mode –
Permissible statements for sequential files<
|INPUT Mode||OUTPUT Mode||I-O Mode||EXTEND Mode|
Permissible statements for indexed and relative files -
|Operation||INPUT Mode||OUTPUT Mode||I-O Mode||EXTEND Mode|
If the FILE STATUS clause is specified, the associated file status key is updated when the OPEN statement is executed. If the file opened successfully, the file status code updated as ZERO (0).
If the file not opened successfully, the file status code updated with the code that describes about the error. The below table shows the file status codes if the file OPEN failed -
|File Opening mode||File Status if Unsuccessful|
|INPUT||Open is unsuccessful. (file status 35)|
|INPUT (optional file)||Normal open; the first read causes the at end condition or the invalid key condition. (file status 05)|
|I-O||Open is unsuccessful. (file status 35)|
|I-O (optional file)||Open causes the file to be created. (file status 05)|
|OUTPUT||Open causes the file to be created.|
|EXTEND||Open is unsuccessful. (file status 35)|
|EXTEND (optional file)||Open causes the file to be created. (file status 05)|
IDENTIFICATION DIVISION. PROGRAM-ID. SEQFILE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT FILE1 ASSIGN TO DISK1. ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS WS-FS. DATA DIVISION. FILE SECTION. RECORD CONTAINS 80 CHARACTERS. BLOCK CONTAINS 800 CHARACTERS. RECORDING MODE IS F. DATA RECORD IS STD-REC. FD FILE1. 01 STD-REC. 02 STD-NO PIC 9(03). 02 STD-NAME PIC X(20). 02 STD-GENDER PIC X(07). 02 FILLER PIC X(50). WORKING-STORAGE SECTION. 77 WS-FS PIC 9(02). 01 WS-EOF-SW PIC X(01) VALUE 'N'. 88 EOF-SW VALUE 'Y'. 88 NOT-EOF-SW VALUE 'N'. PROCEDURE DIVISION. DISPLAY 'SEQUENTIAL FILE READING...'. OPEN INPUT FILE1. PERFORM UNTIL EOF-SW READ FILE1 AT END MOVE 'Y' TO WS-EOF-SW DISPLAY 'CURRENT RECORD : ' STD-REC END-PERFORM. CLOSE FILE1. DISPLAY STD-REC. STOP RUN.