Reading and Writing Files From a Procedure

You may use command procedures to read existing text files and create new ones. Although it is possible to process indexed files with command procedures, only text files will be discussed in this book.

The following DCL commands are used to process files. Note that these commands may also be used from the DCL prompt.

OPEN logical_name file. Before you can read or write a file, you must open it. OPEN /READ opens an existing file for read access. OPEN/WRITE creates a new file. OPEN/APPEND allows you to add data to the end of an existing file. The /SHARE or /SHARE=WRITE qualifier specifies that other users may open the file for read or write access. If you specify /SHARE=READ, other users are allowed to read the file while you have it open, but not to write to it. The /ERROR= label qualifier branches to the specified label if a problem occurs. The logical name is used to identify the file in subsequent READ, WRITE, and CLOSE commands.

CLOSE logical_name. This closes a file you had previously opened.

READ logical_name symbol. This reads the next record from the file specified by logical_name and assigns it to the symbol symbol. Use /END= label to branch to the specified label on reaching the end of the file. Use /ERROR= label to branch to the specified label if a problem occurs.

WRITE logical_name symbol. This writes the value specified by symbol to the file specified by logical_name. Use /ERROR= label to branch to the specified label if a problem occurs.

The following example creates a file, writes a few lines to it, and then reads the file back again:

    $ !    $ ! First, create a file and populate it    $ ! with a few records.    $ !    $ write sys$output "creating file..."    $ open/write testfile demo.txt /error=CANT_OPEN_WRITE    $ write testfile /error=writeerror -            "This is the first line in the file"    $ write testfile /error=writeerror -            "This is the second line in the file"    $ write testfile /error=writeerror -            "This is the third and last line in the file"    $ close testfile    $ !    $ ! Now open the file and read its contents    $ !    $ write sys$output "reading file back:"    $ open/read inputfile demo.txt /error=CANT_OPEN_READ    $ READLOOP:    $   read inputfile inrecord /end=NO_MORE_DATA /error=READERROR    $   write sys$output inrecord    $ goto READLOOP    $ !    $ ! The READ command will branch here when the    $ ! entire file has been read:    $ !    $ NO_MORE_DATA:    $ close inputfile    $ !    $ exit ! End the procedure. Very important: If this    $ !      EXIT is omitted, execution will continue into    $ !      the code below, causing problems.    $ !    $ !-------------------------------------------    $ !    $ ! The OPEN/WRITE command will branch here if the    $ ! file cannot be opened.    $ !    $ CANT_OPEN_WRITE:    $ write sys$output "Cannot open file for writing."    $ exit    $ !    $ ! The OPEN/READ command will branch here if the    $ ! file cannot be opened.    $ !    $ CANT_OPEN_READ:    $ write sys$output "Cannot open file for reading."    $ exit    $ !    $ ! The WRITE command will branch here if the    $ ! file cannot be written.    $ !    $ WRITEERROR:    $ write sys$output "Cannot write the file."    $ close testfile    $ exit    $ !    $ ! The READ command will branch here if the    $ ! file cannot be read.    $ !    $ READERROR:    $ write sys$output "Cannot read the file."    $ close inputfile    $ exit 

When executed, the procedure produces the following output:

     $ @FILES     creating file...     reading file back:     This is the first line in the file     This is the second line in the file     This is the third and last line in the file 

If you omit the error-handling capabilities shown in the example above, the command procedure will produce an error message and abort if an error occurs, but the example becomes much simpler:

     $ !     $ ! First, create a file and populate it     $ ! with some text.     $ !     $ open/write testfile demo.txt     $ write testfile "This is the first line in the file"     $ write testfile "This is the second line in the file"     $ write testfile "This is the third and last line in the file"     $ close testfile     $ !     $ ! Now open the file and read its contents.     $ !     $ open/read inputfile demo.txt     $ readloop:     $   read inputfile inrecord /end=NO_MORE_DATA     $   write sys$output inrecord     $ goto readloop     $ !     $ ! The READ command will branch here when the     $ ! entire file has been read.     $ !     $ NO_MORE_DATA:     $ close inputfile     $ exit     $ ! 

Use caution if you omit the error-handling capabilities. If an error occurs and the procedure aborts, any file open at that time will remain open at the DCL command level. You should attempt to manually close any files the procedure might have opened:

    $ CLOSE TESTFILE    $ CLOSE INPUTFILE 



Getting Started with OpenVMS(c) A Guide for New Users
Getting Started with OpenVMS: A Guide for New Users (HP Technologies)
ISBN: 1555582796
EAN: 2147483647
Year: 2005
Pages: 215

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net