|
HP OpenVMS DCL Dictionary
OPEN
Opens a file for reading, writing, or both; assigns a logical name to a
file; and places the name in the process logical name table.
See the qualifier descriptions for restrictions.
Format
OPEN logical-name[:] filespec
Parameters
logical-name[:]
Specifies the logical name and a character string to be assigned to the
file.
filespec
Specifies the name of the file or device being opened for input or
output. The file type defaults to DAT. The asterisk (*) and the percent
sign (%) wildcard characters are not allowed.
To create a new, sequential file, specify the /WRITE qualifier. See the
description of the /WRITE qualifier for more information.
Description
A file can be opened for either reading or writing, or for both reading
and writing. After the file is opened, it is available for input or
output at the command level with the READ and WRITE commands.
The OPEN command opens files as process permanent. Therefore, these
files remain open until you close them with the CLOSE command, or until
you log out. If a command procedure that opens a file terminates
without closing an open file, the file remains open; the command
interpreter does not automatically close it. The OPEN command uses
OpenVMS RMS to open files, and is subject to RMS restrictions on using
process-permanent files. The OPEN command opens sequential, relative,
or indexed sequential files.
The logical devices SYS$INPUT, SYS$OUTPUT, SYS$COMMAND, and SYS$ERROR
do not have to be opened explicitly before they can be read or written
at the command level. All other files must be opened explicitly.
Do not use the same logical name when you open different files. If you
specify a logical name with the OPEN command and the logical name is
currently assigned to another file, no warning message is issued;
however, the file is not opened, and the next READ request will access
the file to which the logical name was originally assigned.
You can enter more than one OPEN command for the same file and assign
it different logical names if you use the /SHARE qualifier the first
time the file is opened. Also, if you open the file by using the
/SHARE=READ or the /SHARE=WRITE qualifier, other users can access the
file with the TYPE or the SEARCH command.
When you use the OPEN command to create a new file, variable fixed
control (VFC) record format is used. Concatenating a file of this
record format with a file of another record format might be impossible
due to record format incompatibilities. To avoid the VFC format, use
the CREATE command to create the file.
When the OPEN command is specified on an existing file, the record type
of that file is used.
Qualifiers
/APPEND
Opens an existing file for writing and positions the record pointer at
the end-of-file (EOF). New records are added to the end of the file.
Only sequential files allow more than one user to append records
concurrently.
Use the /APPEND qualifier only to add records to an existing file. The
/APPEND and the /WRITE qualifiers are mutually exclusive.
/ERROR=label
Transfers control to the location specified by the label keyword (in a
command procedure) if the open operation results in an error. The error
routine specified for this qualifier overrides any ON condition action
specified. If the /ERROR qualifier is not specified, the current ON
condition action is taken.
If an error occurs and the target label is successfully given control,
the global symbol $STATUS retains the code for the error that caused
the error path to be taken.
/READ (default)
Opens the file for reading. If you open a file with /READ, other users
are also allowed read access to the file, but no user is allowed write
access. If you open a file with /READ/WRITE, no other users are allowed
access while the file is open. If you specify the /READ qualifier
without the /WRITE qualifier, you must specify an existing file.
/SHARE[=option]
/NOSHARE (Alpha/I64 only)
Opens the specified file as a shareable file to allow other users read
or write access. If you specify the /SHARE=READ qualifier, other users
are allowed read (R) access to the file, but not write (W) access. If
you specify the /SHARE=WRITE or the /SHARE qualifier with no option,
users are allowed read and write access to the specified file.
To open a file with no shared access, specify /NOSHARE or use
OPEN/READ/WRITE.
/WRITE
Opens the file for writing. The following restrictions apply to the
/WRITE qualifier:
- Use the /WRITE qualifier to open and create a new, sequential
file. If the file specification on an OPEN/WRITE command does not
include a file version number, and if a file with the specified file
name and file type already exists, a new file with a version number one
greater than the existing file is created.
- Use the /READ qualifier with the /WRITE qualifier to open an
existing file. While the file is open, no other user will have access
to it. When the file is first opened, the pointer is positioned to the
beginning of the file. (This differs from OPEN/APPEND, which positions
the pointer at the end of the file.) You cannot use OPEN/READ/WRITE to
create a new file.
- The /WRITE and the /APPEND qualifiers are mutually exclusive.
Examples
#1 |
$ OPEN INPUT_FILE AVERAGE.DAT
$ READ_LOOP:
$ READ/END_OF_FILE=ENDIT INPUT_FILE NUM
.
.
.
$ GOTO READ_LOOP
$ ENDIT:
$ CLOSE INPUT_FILE
|
The OPEN command opens the file named AVERAGE.DAT as an input file and
assigns it the logical name INPUT_FILE. The file is opened with read
access because the /READ qualifier is present by default. The READ
command reads a record from the logical file INPUT_FILE into the symbol
named NUM. The procedure executes the lines between the labels
READ_LOOP and ENDIT until the end of the file is reached. At the end of
the file, the CLOSE command closes the file.
#2 |
$ OPEN/WRITE/ERROR=OPEN_ERROR OUTPUT_FILE TEMP.OUT
$ COUNT = 0
$ WRITE_LOOP:
$ COUNT = COUNT + 1
$ IF COUNT .EQ. 11 THEN GOTO ENDIT
$ WRITE OUTPUT_FILE "Count is ''COUNT'."
.
.
.
$ GOTO WRITE_LOOP
$ ENDIT:
$ CLOSE OUTPUT_FILE
$ EXIT
$
$ OPEN_ERROR:
$ WRITE SYS$OUTPUT "Cannot open file TEMP.OUT"
$ EXIT
|
The OPEN command with the /WRITE qualifier creates the file TEMP.OUT
and assigns it the logical name OUTPUT_FILE. TEMP.OUT is a sequential
file.
The /ERROR qualifier specifies that if any error occurs while opening
the file, the command interpreter should transfer control to the line
at the label OPEN_ERROR. The command procedure writes records to the
file TEMP.OUT until the symbol COUNT equals 11.
#3 |
$ OPEN/READ INPUT_FILE TRNTO::DKA0:[COST]INVENTORY.DAT
$ READ_LOOP:
$ READ/END_OF_FILE=ENDIT INPUT_FILE NUM
$ FIRST_CHAR = F$EXTRACT(0,1,NUM)
$ WRITE SYS$OUTPUT FIRST_CHAR
$ GOTO READ_LOOP
$ ENDIT:
$ CLOSE INPUT_FILE
|
This command procedure opens the file INVENTORY.DAT located at remote
node TRNTO as an input file, and assigns it the logical name
INPUT_FILE. The READ command reads a record from the logical file
INPUT_FILE into the symbol named NUM. The next two commands extract the
first character from the record and write the character to the
SYS$OUTPUT device. These two steps occur for all records in the file
until the procedure reaches the end-of-file (EOF). At this point, the
CLOSE command closes the file and deassigns the logical name INPUT_FILE.
|