[Up] [Contents] [Index] [Summary]

3.13.1 Input and Output Using Implicit Source and Destination

The package for implicit input and output destination is upwards compatible to DEC-10 and C-Prolog. The reading and writing predicates refer to resp. the current input- and output stream. Initially these streams are connected to the terminal. The current output stream is changed using tell/1 or append/1. The current input stream is changed using see/1. The streams current value can be obtained using telling/1 for output- and seeing/1 for input streams. The table below shows the valid stream specifications. The reserved names user_input, user_output and user_error are for neat integration with the explicit streams.

userThis reserved name refers to the terminal
user_inputInput from the terminal
user_outputOutput to the terminal
user_errorUnix error stream (output only)
<Atom>Name of a Unix file
pipe(<Atom>Name of a Unix command

Source and destination are either a file, one of the reserved words above, or a term `pipe(Command)'. In the predicate descriptions below we will call the source/destination argument `SrcDest'. Below are some examples of source/destination specifications.

?- see(data).%Start reading from file `data'.
?- tell(stderr).%Start writing on the error stream.
?- tell(pipe(lpr)).%Start writing to the printer.

Another example of using the pipe/1 construct is shown below. Note that the pipe/1 construct is not part of Prolog's standard I/O repertoire.

getwd(Wd) :- seeing(Old), see(pipe(pwd)), collect_wd(String), seen, see(Old), atom_chars(Wd, String). collect_wd([C|R]) :- get0(C), C \== -1, !, collect_wd(R). collect_wd([]).

see(+SrcDest)
Make SrcDest the current input stream. If SrcDest was already opened for reading with see/1 and has not been closed since, reading will be resumed. Otherwise SrcDest will be opened and the file pointer is positioned at the start of the file.

tell(+SrcDest)
Make SrcDest the current output stream. If SrcDest was already opened for writing with tell/1 or append/1 and has not been closed since, writing will be resumed. Otherwise the file is created or---when existing---truncated. See also append/1.

append(+File)
Similar to tell/1, but positions the file pointer at the end of File rather than truncating an existing file. The pipe construct is not accepted by this predicate.

seeing(?SrcDest)
Unify the name of the current input stream with SrcDest.

telling(?SrcDest)
Unify the name of the current output stream with SrcDest.

seen
Close the current input stream. The new input stream becomes user.

told
Close the current output stream. The new output stream becomes user.