CLOSE

Example

CLOSE closes a file which has been opened by the READ or WRITE command. Subsequent READ statements will read from the beginning of the file, or subsequent WRITE statements will create a new file. CLOSE can also be used to control access to more than 12 files in a single run, or to insure that a newly updated file will be complete in case the program or computer aborts later.

CLOSE (UNIT=<unit number>, FILE='filename string');

Usage

When TSP processes a READ or WRITE statement which accesses a file, the file is left open so that further READ or WRITE statements will read data from or write data to the next line after those already processed. This is useful when more observations or variables will be read from or written to the file later in the program.

There are several cases in which it may be useful to close the file:

  1. During an interactive session, if an error is made reading or writing data, closing the file will allow correction of the error when the corrected READ or WRITE statements are executed. Without the CLOSE command, a new READ statement would read from the current file position, usually causing an "end of file" error message, while a new WRITE statement would append to the lines already written to the file in error (if any).

  2. User-controlled access to more than 12 files in a given run is possible with CLOSE. Since the number of simultaneously open files is limited on most operating systems (often it is less than 12), TSP will close the most recently opened file and issue a warning message when access to a new file would result in too many simultaneously open files. If this arbitrary choice of the file to close causes problems with your program, use the CLOSE statement to reduce the number of simultaneously open files.

  3. If results from a repeated iterative estimation process are to be saved repeatedly in a file, the CLOSE command could be used to cause repeated creation of the file instead of appending the new results each time to the file. It would be slightly easier to use the OUT command for this type of problem:

OUT databank; KEEP variables; OUT;

  1. If important data has been written to a file, and it is likely that later commands may cause TSP to abort (or power failures may occur with the computer), the file may be closed to guarantee that the data is completely written.

For whatever reason, after the READ or WRITE statement, issue the CLOSE command, specifying the file either with a filename string or with a unit number in the options with the parentheses.

Example

READ (FILE='FOO.DAT') X Y Z;

CLOSE (FILE='FOO.DAT');