HP OpenVMS Systems Documentation |
OpenVMS User's Manual
14.12.2 Using the /INDEX and /KEY QualifiersTo read records randomly from indexed sequential files, use the READ command qualifiers /INDEX and /KEY. These qualifiers specify that a record should be read from the file by finding the specified key in the index and returning the record associated with that key. If you do not specify an index, the primary index (0) is used.
After you read a record randomly, you can read the remainder of the
file sequentially by using READ commands without the /KEY or /INDEX
qualifiers.
You can use the READ command with the /DELETE qualifier to delete records from indexed sequential files. The /DELETE qualifier causes a record to be deleted from a file after it has been read. Use the /DELETE qualifier with the /INDEX and /KEY qualifiers to delete a record specified by a given key.
For more information about the /DELETE, /INDEX, and /KEY qualifiers,
refer to the description of the READ command in the OpenVMS DCL Dictionary.
The CLOSE command closes a file and deassigns the logical name created by the OPEN command. Be sure to close all files you open in a command procedure before the command procedure terminates. If you fail to close an open file, the file remains open when the command procedure terminates and the logical name assigned to the open file is not deleted from the process logical name table. In the following example, the CLOSE command closes the file STATS.DAT and deassigns the logical name INFILE:
14.14 Modifying FilesThis section describes three methods of modifying files:
14.14.1 Updating RecordsWhen you use the updating method to modify records, you can make minor changes to a small number of records in a file. Because this method does not allow you to change the size of a record or the number of records in the file, use it only for files with formatted records (for example, in a data file). To make minor changes in a file, use this procedure:
The following command procedure shows how to make changes to a sequential file by reading and updating individual records:
The system displays the record on the terminal and you are asked
whether the record needs to be modified. If you choose to modify the
record, a new record is read from the terminal and its length is
compared to the length of the original record. If the original record
is longer, extra spaces make the new record the same size. If the
original record is shorter, the system displays an error message and
you are again prompted for a new record.
To make extensive changes to a file, open that file for read access and open a new file for write access. Because you are creating a new output file, you can modify the size of records, add records, delete records, or insert records. The OPEN/WRITE command opens a new file for write access. The new file can have the same name as the original file and a version number one higher than the version number of the old file.
To create files that you can modify, use the following procedure:
In the following table, the symbol RECORD contains the record read from the original file:
Examples: Modifying Records
Example: Creating Output Files
The following example shows a command procedure that reads a record from an input file, processes the record, and copies the record into an output file:
14.14.3 Appending Records to FilesUse the following procedure (OPEN/APPEND command) to append records to the end of existing files:
The following command procedure appends records to the end of the file named STATS.DAT:
14.15 Handling File I/O ErrorsUse the /ERROR qualifier with the OPEN, READ, or WRITE command to suppress system error messages and to pass control to a specified label. If an error occurs during an input or output operation, the /ERROR qualifier overrides all other error-control mechanisms (except the /END_OF_FILE qualifier on the READ command). The following example uses the /ERROR qualifier with the OPEN command:
The OPEN command requests that the file CONTINGEN.DOC be opened for reading. If the file cannot be opened (for example, if the file does not exist), the OPEN command returns an error condition and transfers control to the CHECK label. The error path specified by the /ERROR qualifier overrides the current ON condition established for the command level. If an error occurs and the target label is successfully given control, the reserved global symbol $STATUS retains the code for the error. You can use the F$MESSAGE lexical function in your error-handling routine to display the message in $STATUS. In the following example, the lexical function F$MESSAGE is used to display the contents of the F$STATUS lexical:
14.15.1 Default Error ActionsIf an error occurs while you are using the OPEN, READ, WRITE, or CLOSE command and you do not specify an error action, the current ON command action is taken. When a READ command receives an end-of-file message, the error action is determined as follows:
14.16 Techniques for Controlling Execution FlowThe normal flow of execution in a command procedure is sequential: the commands in the procedure execute in order until the end of the file is reached. However, you can also control whether certain statements are executed or the conditions under which the procedure should continue executing. The following sections discuss:
14.16.1 Using the IF CommandThe IF command tests the value of an expression and executes a command or block of commands when the result of the expression is true. When the result of the expression is false, one of the following occurs:
DCL provides two distinct formats for the IF command. The first format executes a single command when the expression specified to the IF command is true, as discussed in Chapter 13.
DCL also provides a block-structured IF format. The block-structured IF
command executes more than one command if the expression specified is
true and accepts an optional ELSE statement, which executes one or more
commands if the expression is false.
To execute more than one command when an expression is true, specify the THEN command as a verb (a DCL command preceded by a dollar sign) and terminate the resulting block-structured statement with an ENDIF statement. In the following example, the THEN statement is used as a verb:
14.16.3 Using the ELSE CommandTo execute one or more commands when an expression is false, specify the ELSE statement as a verb and terminate the resulting block-structured statement with an ENDIF statement. In the following example, the ELSE command is used as a verb:
14.16.4 Using Command BlocksCommand blocks can be executed in several ways, depending on whether you leave the commands in the same command procedure or put them in another command procedure and execute them there. The guidelines are as follows:
You can execute a block of commands after the THEN command when the result of the IF expression is true. When you use a block of commands, place the THEN command as the first command on the line following the IF command. In the following example, two SET TERMINAL commands execute and the procedure transfers control to the label PROCEED when F$MODE equals "INTERACTIVE". When F$MODE does not equal "INTERACTIVE", the procedure exits.
The following example illustrates how to use a block of commands with the IF command in conjunction with the ELSE command:
When the condition is true, the procedure writes a message to SYS$OUTPUT and executes the SHOW DEVICE and SET DEVICE commands. When the condition is not true, the procedure writes two messages to SYS$OUTPUT. When you use the IF-THEN-ELSE language construct, observe the following restrictions:
The expression following the IF command can consist of one or more numeric constants, string literals, symbolic names, or lexical functions separated by logical, arithmetic, or string operators. An expression is true when it has one of the following values:
An expression is false when it has one of the following values:
When you write an expression for an IF command, adhere to the following rules:
The following examples illustrate expressions that can be used with the IF command. For additional examples, refer to the description of the IF command in the OpenVMS DCL Dictionary. The first example uses a logical operator and executes only one command following the THEN statement. When the symbol CONT is not true, the procedure exits.
The following example uses a symbol and a label within the IF expression:
When the symbol CHANGE is true, the procedure transfers control to the label GET_CHANGE. Otherwise, the procedure executes the command following the IF command. The next example illustrates two different IF commands:
The first IF command compares two integers; the second IF command compares two strings. Note that the .EQ. operator is used for the integer comparison and the .EQS. operator is used for the string comparison. First, the value of COUNT is compared to the integer 9. When the values are equal, the procedure exits; when the values are not equal, the procedure continues. The loop terminates after eight parameters (the maximum number allowed) have been processed. In the second IF expression, the string value of the symbol P'COUNT' is compared to a null string to see whether the symbol is undefined. Note that you must use apostrophes to force iterative substitution of the symbol COUNT. For example, when COUNT is 2, the result of the first translation is P2. Then, the value of P2 is used in the string comparison. You can also execute a separate command procedure when the result of the IF expression is true. The following example executes the command procedure EXIT_ROUTINE.COM when the result of the IF expression is true:
|