[top] [up] [next]

Streams


Input and output of data is based on sources and sinks. A source is an entity from which input is read and a sink is something to which data can be written. A source of data can be a terminal with a keyboard or a file. A sink of data can be file or a console window. Each source/sink is associated with a finite or potentially infinite sequence of characters or bytes. A source/sink always has a beginning but it only has an end when it is finite. A source/sink can be opened by the built-in predicates open/3 or open/4 and closed with close/1 or close/2. A Prolog program can ouput results (bytes, characters, character codes, terms) to a sink or input Prolog data (bytes, characters, character codes, terms) from a source.

Stream selection and control

Streams provide a logical view of the sources and sinks. A source can be regarded as an input stream and a sink is an output stream. Two streams are predefined and always open during the execution of every goal. The standard input stream is connected to the keyboard and the standard ouput stream writes to the console window.

Any stream may be associated with an alias which is an atom given by the user which to refer to that stream. If a stream is opened a stream-term is generated which identifies the stream, this stream-term is generated by Trinc-Prolog. A particular alias will refer to at most one stream at any time.

  %Open a binary output stream with an alias
  :- open('c:\\temp\\output.txt', write, StreamTerm, [alias(out), type(binary)]).

Two streams are predefined and always open, these standard streams have the aliases user_input and user_output. The stream user_input can read input from the keyboard and the stream user_output writes data to the console window. They are the default current input and current output streams. I/O predicates that do not have an argument for specifying the stream use either the current input or the current output stream (this depends on the predicate). The current input and output stream can be redirected by the predicates set_input/1 and set_output/1. Which streams are the current input and ouput streams can be determined by the predicates current_input/1 and current_output/1. When the current input (output) stream is closed the standard input (output) stream becomes the current input (output) stream.

  %Set the current output stream
  :- set_output(out).
  %write a byte to the current output stream
  :- put_byte(67).

[top] [up] [next]

 

info@trinc-prolog.com