However, you might wish to get input/read from somewhere else
during the execution of your Prolog program - for example,
you might want to read from a file held on the computer on
which the program is running, or on some local file server.
To change the current input stream, you use the Prolog
built-in extra-logical predicate
see. If Prolog
executes the goal
see('input.dat'), then input will subsequently
come from the file
input.dat, in the current working directory
of the workstation*
that is running Prolog. If the specified file cannot be found in the
current working directory, an error may be reported,
as in this interaction with SWI Prolog:
?- see('nosuchfile.dat'). ERROR: see/1: source_sink `nosuchfile.dat' does not exist (No such file or directory)
Other errors are possible - you may not have the right to read
the file in question, for example.
If the file does exist and is readable, then subsequent read operations
get their data from the file. The parameter to
see can be
just the file name, as illustrated above, or it could be
a path to the file that is wanted: e.g.
Prolog will continue to issue prompts for more queries while
you are "seeing" a file, but any explicit read operations
access the file.
The built-in extra-logical predicate
seen (with no argument)
allows you to revert to reading data from the keyboard.
info.dat starts with the line
?- see('info.dat'), read(X). X = hungry(jack) ?- seen, read(Y). |: full(jack). Y = full(jack)
See also current output stream, input, output, files.
input.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.