Guardian Programmer's Guide

Table Of Contents
Using the File System
Guardian Programmer’s Guide 421922-014
2 - 29
Closing Files
Closing Files
You can close files explicitly using the FILE_CLOSE_ procedure.
ERROR := FILE_CLOSE_(FILENUM);
If you do not explicitly close a file, the file remains open until the process stops. When
a process stops, all files that the process has open are automatically closed.
Once you have closed a file, the file number can no longer access that file. The file
number is now available to be reassigned to another file.
Accessing Files: An Example
The following simple program uses many of the procedure calls described in this
section. The program shows communication with a terminal and with an unstructured
disk file.
The program is designed to keep a daily log of comments. It allows the user to append
comments to the log or read comments from the log.
The program prompts a user to request one of these functions:
Append a record to the disk file. Records are 512 bytes long and are terminated
when the line-termination character is entered.
Read a record from the disk file and display it on the terminal. Read operations
begin at the first record in the file. The program prompts the user to make
additional read requests. Successive read operations display records sequentially.
Exit the program.
Before running the program, the data file to contain the log must exist. You can create
this file either programmatically by using the FILE_CREATE_ procedure as described
earlier in this section or interactively using either the CREATE command or the FUP
CREATE command. The following example uses the FUP CREATE command:
1> FUP
-CREATE $ADMIN.OPERATOR.LOGFILE
CREATED - $ADMIN.OPERATOR.LOGFILE
-EXIT
2>
The program consists of the following procedures:
The LOGGER procedure is the main procedure. It calls INIT to handle the Startup
messages and open files. It calls the GET^COMMAND procedure to prompt the
user for the function to perform and then calls the appropriate procedure to
execute the selected function. If the user selected “r,” the LOGGER procedure
calls READ^RECORD. If the user selected “a,” the LOGGER procedure calls
APPEND^RECORD. If the user selected “x,” the LOGGER procedure calls
EXIT^PROGRAM.