An implicit opening of a file occurs when a GET, PUT, READ, WRITE, LOCATE, REWRITE, or DELETE statement is executed for a file for which an OPEN statement has not already been executed.
If a GET statement contains a COPY option, execution of the GET statement can cause implicit opening of either the file specified in the COPY option or, if no file was specified, of the output file SYSPRINT. Implicit opening of the file specified in the COPY option implies the STREAM and OUTPUT attributes.
Table 32 shows the attributes that are implied when a given statement causes the file to be implicitly opened:
| Statement | Implied Attributes |
|---|---|
| GET | STREAM, INPUT |
| PUT | STREAM, OUTPUT |
| READ | RECORD, INPUTNote |
| WRITE | RECORD, OUTPUTNote |
| LOCATE | RECORD, OUTPUT, SEQUENTIAL |
| REWRITE | RECORD, UPDATE |
| DELETE | RECORD, UPDATE |
|
Note:
INPUT and OUTPUT are default attributes for READ and WRITE statements only if UPDATE has not been explicitly declared. |
|
When one of the statements listed in Table 32 opens a file implicitly, it is functionally equivalent to using an explicit OPEN statement for the file with the same attributes specified.
There must be no conflict between the attributes specified in a file declaration and the attributes implied as the result of opening the file. For example, the attributes INPUT and UPDATE are in conflict, as are the attributes UPDATE and STREAM.
The implied attributes discussed earlier are applied before the default attributes listed in Table 32 are applied. Implied attributes can also cause a conflict. If a conflict in attributes exists after the application of default attributes, the UNDEFINEDFILE condition is raised.
| Merged Attributes | Implied Attributes |
|---|---|
| UPDATE | RECORD |
| SEQUENTIAL | RECORD |
| DIRECT | RECORD, KEYED |
| OUTPUT, STREAM | |
| KEYED | RECORD |
The following two examples illustrate attribute merging for an explicit opening using a file constant and a file variable:
declare Listing file stream; open file(Listing) print;
Attributes after merge caused by execution of the OPEN statement are STREAM and PRINT. Attributes after implication are STREAM, PRINT, and OUTPUT. Attributes after default application are STREAM, PRINT, OUTPUT, and EXTERNAL.
declare Account file variable,
(Acct1,Acct2) file
output;
Account = Acct1;
open file(Account) print;
Account = Acct2;
open file(Account) record unbuf;
The file Acct1 is opened with attributes (explicit and implied) STREAM, EXTERNAL, PRINT, and OUTPUT. The file Acct2 is opened with attributes RECORD, EXTERNAL, and OUTPUT.
declare Master file keyed internal;
read file (Master)
into (Master_Record)
keyto(Master_Key);
Attributes after merge (from the implicit opening caused by execution of the READ statement) are KEYED, INTERNAL, RECORD, and INPUT. (No additional attributes are implied.) Attributes after default application are KEYED, INTERNAL, RECORD, INPUT, and SEQUENTIAL.
declare File3 input direct environment( regional(1) )
This declaration specifies three file attributes: INPUT, DIRECT, and ENVIRONMENT. Other implied attributes are FILE (implied by each of the attributes) and RECORD and KEYED (implied by DIRECT). Scope is EXTERNAL, by default. The ENVIRONMENT attributes specifies that the data set is of the REGIONAL(1) organization.
For the previous declaration, all necessary attributes are either stated or implied in the DECLARE statement. None of the stated attributes can be changed (or overridden) in an OPEN statement.
If the declaration is written as shown in the following example, invntry can be opened for different purposes.
declare invntry file;
In the following example, the file attributes are the same as those specified (or implied) in the DECLARE statement in the previous example.
open file (Invntry)
update sequential;
The file might be opened in this way, then closed, and then later opened with a different set of attributes. For example, the following OPEN statement allows records to be read with either the KEYTO or the KEY option.
open file (Invntry)
input sequential keyed;
Because the file is SEQUENTIAL, the data set can be accessed in a purely sequential manner. It can also be accessed directly by means of a READ statement with a KEY option. A READ statement with a KEY option for a file of this description obtains a specified record. Subsequent READ statements without a KEY option access records sequentially, beginning with the next record in KEY sequence.