OPEN Statement
Selects a file for subsequent input, output or update.
Syntax
OPEN {fileModifier,}fileSpec {TO fileVar} {SETTING settingVar} [THEN statement(s) | ELSE statement(s)]
Syntax Elements
fileModifier An expression that evaluates to one of the following strings:
DATA Explicitly specifies the data section of fileSpec.
D{ICT} Specifies the dictionary section of fileSpec. The single character 'D' can be used as an abbreviation for "DICT".
The default, if omitted, is the data section.
fileSpec An expression that evaluates to the file specifier of the file to be opened.
Note
Do not specify the dictionary of the file in both fileModifier and fileSpec.
fileVar The name of a variable to which a reference to the opened file is assigned.
If TO fileVar is specified, a reference to the dictionary or data section of the file is assigned to the specified variable for subsequent use. The file variable can be passed to other programs to eliminate the need to open a file many times.
If the TO fileVar is omitted, an internal default file variable is generated. Subsequent I/O statements that do not specify a file variable then automatically default to this file.
settingVar The name of a variable that is set to a value corresponding to a file I/O error code by the SETTING clause if the file is not opened and the ELSE clause is taken. The file I/O error codes are described in File I/O and IPC Error Codes. If the ELSE clause is not taken, settingVar is set to 0.
statement(s) Either a THEN or ELSE clause (or both). A statement must be included.
Note
This behaviour can be changed with the NO.IO.ERR compatibility option.
-
The THEN clause is executed when the file is open.
-
The ELSE clause is executed if the specified file does not exist.
Operation
If a file is retrieval protected and security codes do not match, an error message is displayed and the program terminates. Update-protected files can be opened if update codes do not match, but attempted writes to the file display an error message and cause entry to debug. Refer to Programming in DataBasic for related information about efficiency considerations.
Number of Open Files
There is for all databases an overall limit on the total number of different data sections (including indexes) that can be open. This is set for each database by the config file parameter NumDataSects, which defaults to 400, but can be changed.
Examples
OPEN 'DICT','QA4' TO F1 ELSE PRINT "NO FILE"; STOP
Opens the dictionary portion of file QA4 and assigns it to file variable F1. If QA4 does not exist, the message NO FILE is displayed and the program terminates.
OPEN 'ABC' TO D5 ELSE STOP 201, "ABC" END
Opens data section of file ABC and assigns it to variable D5. If ABC does not exist, an error message displays and the program terminates.
OPEN '', 'TEST' ELSE PRINT "DOES NOT EXIST"; G0 10
Opens data section of file TEST and assigns it to an internal default file variable. If TEST does not exist, an error message displays and control transfers to statement 10.
OPEN 'TEST' ELSE PRINT "DOES NOT EXIST"; G0 10
This example functions identically to the one above.
OPEN ABC TO HOLD.IT SETTING WHY ELSE GO 75
This example opens the file named in variable 'ABC' and assigns it to the file variable named 'HOLD.IT". If the file is successfully opened, the value of 'WHY' is zero. If the file cannot be opened, the value of 'WHY' is set to an error code value and a branch is taken to the statements following statement label 75.
OPEN 'SALES,1991' ELSE STOP
Opens data section 1991 of the file SALES and assigns the default file variable. If SALES,1991 cannot be opened, the program stops executing.
PARAM = 'DICT /PRODUCTION/INVENTORY'
OPEN PARAM TO PI95 ELSE GOSUB 99
Opens the dictionary of file INVENTORY, which is defined on account PRODUCTION. Calls internal subroutine 99 if the file cannot be opened.