78 Logic Programming With Prolog
Output to a File
Although the above definition of tell states that 'any existing file with the same
name is deleted', there is another possibility, which is important for some
applications, namely that the file is not deleted and any output is placed after the
end of the existing contents of the file. Both the 'overwrite' and the 'append' options
are likely to be available in any practical implementation of Prolog but may
involve using a different predicate (e.g. open) instead of or as well as tell. See the
documentation of your version of Prolog for details.
5.9 File Input: Changing the Current Input Stream
The current input stream can be changed using the see/1 predicate. This takes a
single argument, which is an atom or variable representing a file name, e.g.
see('myfile.txt').
Evaluating a see goal causes the named file to become the current input stream.
If the file is not already open it is first opened (for read access only). If it is not
possible to open a file with the given name, an error will be generated.
Note that the file corresponding to the previous current input stream remains
open when a new current input stream is selected. Only the current input stream
can be closed (using the seen predicate described below).
The default current input stream is user, i.e. the user's terminal. This value can
be restored either by using the seen predicate or by see(user).
The built-in predicate seen/0 takes no arguments. Evaluating a see goal causes
the current input file to be closed and the current input stream to be reset to user,
i.e. the user's terminal.
The built-in predicate seeing/1 takes one argument, which must be a variable
and will normally be unbound. Evaluating a seeing goal causes the variable to be
bound to the name of the current input stream.
5.9.1 Reading from Files: End of File
If the end of file is encountered when evaluating the goal read(X), variable X will
be bound to the atom end_of_file.
If the end of file is encountered while evaluating the goal get(X) or get0(X),
variable X will be bound to a 'special' numerical value. As ASCII values must be in
the range 0 to 255 inclusive, this will typically be -1, but may vary from one
implementation of Prolog to another.