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

3.13.2 Explicit Input and Output Streams

The predicates below are part of the Quintus compatible stream-based I/O package. In this package streams are explicitly created using the predicate open/3. The resulting stream identifier is then passed as a parameter to the reading and writing predicates to specify the source or destination of the data.

open(+SrcDest, +Mode, -Stream, +Options)
ISO compliant predicate to open a stream. SrcDes is either an atom, specifying a Unix file, or a term `pipe(Command)', just like see/1 and tell/1. Mode is one of read, write, append or update. Mode append opens the file for writing, positioning the file-pointer at the end. Mode update opens the file for writing, positioning the file-pointer at the beginning of the file without truncating the file. See also stream_position/3. Stream is either a variable, in which case it is bound to an integer identifying the stream, or an atom, in which case this atom will be the stream identifier. The Options list can contain the following options:

type(Type)
Using type text (default), Prolog will write a text-file in an operating-system compatible way. Using type binary the bytes will be read or written without any translation. Note there is no difference between the two on Unix systems.

alias(Atom)
Gives the stream a name. The following two calls are identical, but only the latter is allowed in ISO Prolog. ?- open(foo, read, in, []). ?- open(foo, read, S, [alias(in)]).

eof_action(Action)
Defines what happens if the end of the input stream is reached. Action eof_code makes get0/1 and friends return -1 and read/1 and friends return the atom end_of_file. Repetitive reading keeps yielding the same result. Action error is like eof_code, but repetitive reading will raise an error. With action reset, Prolog will examine the file again and return more data if the file has grown.

The option reposition is not supported in SWI-Prolog. All streams connected to a file may be repositioned.

open(+SrcDest, +Mode, ?Stream)
Equivalent to open/4 with an empty option-list.

open_null_stream(?Stream)
Open a stream that produces no output. All counting functions are enabled on such a stream. An attempt to read from a null-stream will immediately signal end-of-file. Similar to Unix /dev/null.

close(+Stream)

Close the specified stream. If Stream is not open an error message is displayed. If the closed stream is the current input or output stream the terminal is made the current input or output.

current_stream(?File, ?Mode, ?Stream)
Is true if a stream with file specification File, mode Mode and stream identifier Stream is open. The reserved streams user and user_error are not generated by this predicate. If a stream has been opened with mode append this predicate will generate mode write.

stream_position(+Stream, -Old, +New)
Unify the position parameters of Stream with Old and set them to New. A position is represented by the following term: '$stream_position'(CharNo, LineNo, LinePos).

It is only possible to change the position parameters if the stream is connected to a disk file. If the position is changed, the CharNo field determines the new position in the file. The LineNo and LinePos are copied in the stream administration.