However, you might wish to write to somewhere else during the execution of your Prolog program - for example, you might want to write to a file held on the computer on which the program is running, or on some local file server.
To change the current output stream, you use one of the Prolog
built-in extra-logical predicates
If Prolog executes the goal
tell('output.dat'), then output
will subsequently go to the file
in the current working directory of the
that is running Prolog.
If the specified file cannot be found in the
current working directory, it will be created. If the file does exist, it will
be overwritten. If you use
write operations will add material to the end of the file,
instead of overwriting the file. If you do not have permission
to write files in the current directory, you will see an error
?- tell('/usr/bin/ztrash'). ERROR: tell/1: No permission to open source_sink `/usr/bin/ztrash' (Permission denied)
This means that either you do not have permission to write files in
/usr/bin, or if the file
already exists in this directory,
that you do not have permission to write to that file.
If the file is able to be written, then subsequent write operations
send their data to the file. The parameter to
tell can be
a path to the file that is wanted, as in the example above, or it
could be just the file name, e.g.
Prolog will continue to issue prompts for more queries and
print bindings while you are "telling" or "appending" a file,
but any explicit write operations access the file.
The built-in extra-logical predicate
told (with no argument)
allows you to revert to writing data to the original window.
?- tell('info.dat'), write(thirsty(jack)), nl. true. ?- told, write(drunk(jack)). drunk(jack) true. ?- halt. % cat info.dat # - # is Unix comment char, cat lists info.dat thirsty(jack) %
See also current input stream, input, output, files.
append/1 is not related to
append/3, which in turn
has nothing to do with output streams.
# Strictly speaking,
output.dat will be expected to be in
the current working directory of the command interpreter
that started Prolog. The command interpreter will be running
on the workstation/computer, and sending output to a window on
that workstation or computer.