Programmer Guide > Working with Data Files > Opening and Closing Files
  

Opening and Closing Files
PV-WAVE has several commands for opening and closing data files; you select the command that matches the way you intend to use the file.
Opening Files
Before a file can be processed by PV-WAVE, it must be opened and associated with a number called the logical unit number, or LUN for short. All I/O is done by specifying the LUN, not the filename.
The LUN is supplied as part of the function call. For example, to open the file named data.dat for reading on file unit 1, you would enter the following command:
OPENR, 1, 'data.dat'
Once the file is opened, you can choose between several I/O routines. Each routine fills a particular need—the one to use depends on the particular situation. Refer to the examples in this chapter to get an idea of how (and when) to open and close data files.
 
note
If you are using one of the I/O routines that start with the letters “DC”, you do not need to explicitly open and close the file, because these steps happen automatically. For more details, refer to "Functions for Simplified Data Connectivity".
Basic Commands for Opening Files
The three main OPEN commands are listed in the OPEN Commands:
Table 9-1: OPEN Commands
Procedure
Description
OPENR
Opens an existing file for input only.
OPENW
Opens a new file for input and output. Under UNIX and Windows, if the named file already exists, the previous contents are destroyed.
OPENU
Opens an existing file for input and output.
The general form for using any of the OPEN procedures is:
OPENx, unit, filename
where unit refers to the logical file unit that will be allocated for opening the file named filename, and x is either an R, W, or U, depending on which of the three OPEN commands you choose to use.
 
note
The three commands shown above recognize keywords that modify their normal behavior. Some keywords are generally applicable, while others only have effect under a given operating system. For more information about keywords, refer to the descriptions for the OPENR, OPENW, and OPENU procedures. These descriptions can be found in the PV‑WAVE Reference.
When to Open the File for I/O (Input/Output)
Usually you must open the file before any I/O can be performed. But there are two situations where you don’t need to open the file before doing any I/O:
*Reserved LUNs—There are three file units that are always open—in fact, the user is not allowed to close them. These files are standard input (usually the keyboard), standard output (usually the workstation’s screen), and standard error output (usually the workstation’s screen). These three files are associated with LUNs 0, –1, and –2 respectively. Because these file units are always open, you do not need to open them prior to using them for I/O. For more information about the three reserved file units, refer to "Reserved Logical Unit Numbers (–2, –1, 0)".
*Simplified I/O Routines—Any I/O function that begins with the two letters “DC” automatically handles the opening and closing of the file unit. This group of functions has been provided to simplify the process of getting your data in and out of PV-WAVE. For more information about the DC I/O functions, refer to "Functions for Simplified Data Connectivity".
Closing Files
Always close the file when you are done using it. Closing a file removes the association between the file and its LUN and thus frees the LUN for use with a different file. There is usually an operating-system-imposed limit on the number of files you may have open at once. Although this number is large enough that it rarely causes problems, you may occasionally need to close a file before opening another file. In any event, it is a good idea to only keep needed files open.
Closing a LUN is done with the CLOSE procedure. For example, to close file unit 1, enter this command:
CLOSE, 1
Also, remember that PV-WAVE closes all open files as it shuts down. Any LUN you allocated is automatically deallocated when you exit PV-WAVE with the EXIT or QUIT command.
 
note
If FREE_LUN is called with a file unit number that was previously allocated by GET_LUN, it calls CLOSE before deallocating the file unit.

Version 2017.0
Copyright © 2017, Rogue Wave Software, Inc. All Rights Reserved.