14.6.1 Opening Files
The OPEN statement opens a file for processing, specifies
the characteristics of the file to RMS, and verifies the result.
Opening a file with the specification FOR INPUT specifies
that you want to use an existing file. Opening a file with the
specification FOR OUTPUT indicates that you want to cre-
ate a new file. If you do not specify FOR INPUT or FOR
OUTPUT, BASIC tries to open an existing file. If no such file
exists, a new file is created.
Clauses to the OPEN statement allow you to specify the
characteristics of a file. All OPEN statement clauses con-
cerning file or record format are optional when you open an
existing file; those attributes that are not specified default to
the attributes of the existing file. When you open an existing
file, you must specify the file name, channel number, and un-
less the file is a terminal-format file, an organization clause.
If you do not know the organization of the file you want to
open, you can specify ORGANIZATION UNDEFINED. If
you specify ORGANIZATION UNDEFINED, also specify
RECORDTYPE ANY.
If you do not specify a map in the OPEN statement, the size
of your program's record buffer is determined by the OPEN
statement RECORDSIZE clause, or by the record size asso-
ciated with the file. If you specify both a MAP clause and a
RECORDSIZE clause in the OPEN statement, the specified
record size overrides the size specified by the MAP clause.
The following statement opens a new sequential file of stream
format records:
OPEN "TEST.DAT" FOR OUTPUT AS FILE #1%, &
SEQUENTIAL STREAM
The following example creates a relative file and associates
it with a static record buffer. The MAP statement defines
the record buffer's total size and the data types of its vari-
ables. When the program is compiled, BASIC allocates space
in the record buffer for one integer, one 16-byte string, and
one double-precision, floating-point number. The record size
is the total of these fields, or 28 bytes. All subsequent record
operations use this static buffer for I/O to the file.
MAP (Inv_item) LONG Part_number, &
STRING Inv_name = 16, &
DOUBLE Unit_price
OPEN "INVENTORY.DAT" FOR OUTPUT AS FILE #1% &
,ORGANIZATION RELATIVE FIXED, ACCESS MODIFY &
,ALLOW READ, MAP Inv_item
The following OPEN statement opens a sequential file for
reading only (ACCESS READ). Because the OPEN statement
does not contain a MAP clause, a record buffer is created.
This record buffer is 100 bytes long.
OPEN "CASE.DAT" AS FILE #1% &
,ORGANIZATION SEQUENTIAL VARIABLE &
,ACCESS READ &
,RECORDSIZE 100%
When you do not specify a MAP statement, your program
must use MOVE TO and MOVE FROM statements to move
data between the record buffer and a list of variables.
The OPEN statement for indexed files must have a MAP
clause. Moreover, if you are creating an indexed file, a
PRIMARY KEY clause is required. You can create a seg-
mented index key containing more than one string variable
by separating the variables with commas and enclosing them
in parentheses. All the string variables must be part of the
associated map.
In the following example, the primary key is made up of
three string variables. This key causes the records to be
sorted in alphabetical order according to the user's last name,
first name, and middle initial.
MAP (Segkey) STRING First_name = 15, MI = 1, Last_name = 15
OPEN "NAMES.IND" FOR OUTPUT AS FILE #1%, &
ORGANIZATION INDEXED, &
PRIMARY KEY (Last_name, First_name, MI), &
MAP Segkey
Note that there are restrictions on the maximum record
size allowed for various file and record formats. See the
OpenVMS Record Management Services Reference Manual
for more information.
You can use logical names to assign a mnemonic name to all
or part of a complete file specification, including node, device,
and directory. The advantage in using logical names is that
programs do not depend on literal file specifications. You can
define logical names from the following:
.
From DCL command level with the ASSIGN or DEFINE
command
.
From within a program with the SYS$CRELMN system
service
.
From within the BASIC environment with the BASIC
command ASSIGN
BASIC supports any valid logical name as part of a file
specification.
A logical name specifies a 1- to 255-character name to be
associated with the specified device or file specification. If
the logical name specifies a device, you must end the logical
name with a colon. The following example defines a logical
name for a file specification:
$ ASSIGN DUA1:[SENDER]PAYROL.DAT PAYROLL_DATA
This example defines a logical name for a physical device:
$ ASSIGN DUA2: DISK2:
Once you define the logical name, you can reference that
name in your program. For example:
OPEN "PAYROLL_DATA" FOR INPUT AS FILE #1%, &
ORGANIZATION SEQUENTIAL
OPEN "DISK2:[SORT_DATA] SORT.LIS" FOR OUTPUT AS FILE #2%, &
SEQUENTIAL VARIABLE
These OPEN statements do not depend on the availabil-
ity of DUA1: or DUA2: in order to work. If these devices
are not available, you can redefine the logical names so that
they specify other disk drives before running the program.
In addition, you can redirect the entire file specification for
PAYROLL_DATA to point to the test or production version of
the data.