Previous Contents Next
6.10 Streams

6.10.1 Introduction
A stream provides a logical view of a source/sink.

Sources and sinks: a program can output results to a sink or input data from a source. A source/sink may be a file (regular file, terminal, device,...), a constant term, a pipe, a socket,...

Associating a stream to a source/sink: to manipulate a source/sink it must be associated to a stream. This provides a logical and uniform view of the source/sink whatever its type. Once this association has been established, i.e. a stream has been created, all subsequent references to the source/sink are made by referring the stream. A stream is unidirectional: it is either an input stream or an output stream. For a classical file, the association is done by opening the file (whose name is specified as an atom) with the open/4 (section 6.10.6). GNU Prolog makes it possible to treat a Prolog constant term as a source/sink and provides built-in predicates to associate a stream to such a term (section 6.11). GNU Prolog provides operating system interface predicates defining pipes between GNU Prolog and child processes with streams associated to these pipes, e.g. popen/3 (section 6.27.21). Similarly, socket interface predicates associate streams to a socket to allow the communication, e.g. socket_connect/4 (section 6.28.5).

Stream-term: a stream-term identifies a stream during a call of an input/output built-in predicate. It is created as a result of associating a stream to a source/sink (section above). A stream-term is a compound term of the form '$stream'(I) where I is an integer.

Stream aliases: any stream may be associated with a stream alias which is an atom which may be used to refer to that stream. The association automatically ends when the stream is closed. A particular alias only refers to at most one stream at any one time. However, more than one alias can be associated to a stream. All built-in predicates which have a stream-term as an input argument also accept a stream alias as that argument. However, built-in predicates which return a stream-term do not accept a stream alias.

Standard streams: two streams are predefined and open during the execution of every goal: the standard input stream which has the alias user_input and the standard output stream which has the alias user_output. A goal which attempts to close either standard stream succeeds, but does not close the stream.

Current streams: during execution there is a current input stream and a current output stream. By default, the current input and output streams are the standard input and output streams, but the built-in predicates set_input/1 (section 6.10.4) and set_output/1 (section 6.10.5) can be used to change them. When the current input stream is closed, the standard input stream becomes the current input stream. When the current output stream is closed, the standard output stream becomes the current output stream.

Text streams and binary streams: a text stream is a sequence of characters. A text stream is also regarded as a sequence of lines where each line is a possibly empty sequence of characters followed by a new line character. GNU Prolog may add or remove space characters at the ends of lines in order to conform to the conventions for representing text streams in the operating system. A binary stream is a sequence of bytes. Only a few built-in predicates can deal with binary streams, e.g. get_byte/2 (section 6.13).

Stream positions: the stream position of a stream identifies an absolute position of the source/sink to which the stream is connected and defines where in the source/sink the next input or output will take place. A stream position is a ground term of the form '$stream_position'(I1, I2, I3, I4) where I1, I2, I3 and I4 are integers. Stream positions are use to reposition a stream (when possible) using for instance set_stream_position/2 (section 6.10.13).

The position end of stream: when all data of a stream S has been input S has a stream position end-of-stream. At this stream position a goal to input more data will return a specific value to indicate that end of stream has been reached (e.g. -1 for get_code/2 or end_of_file for get_char/2,...). When this terminating value has been input, the stream has a stream position past-end-of-stream.

Buffering mode: input/output on a stream can be buffered (line-buffered or block-buffered) or not buffered at all. The buffering mode can be specified at open time or using set_stream_buffering/2 (section 6.10.24). Line buffering is used on output streams, output data are only written to the sink when a new-line character is output (or at the close time). Block buffering is used on input or output. On input streams, when an input is requested on the source, if the buffer is empty, all available characters are read (within the limits of the size of the buffer), subsequent reads will first use the characters in the buffer. On output streams, output data are stored in the buffer and only when the buffer is full is it physically written on the sink. Thus, an output to a buffered stream may not be sent immediately to the sink connected to that stream. When it is necessary to be certain that output has been delivered, the built-in predicate flush_output/1 (section 6.10.8) should be used. Finally, it is also possible to use non-buffered streams, in that case input/output are directly done on the connected source/sink. This can be useful for communication purposes (e.g. sockets) or when a precise control is needed, e.g. select/5 (section 6.27.23).

6.10.2 current_input/1

Templates

current_input(?stream)
Description

current_input(Stream) unifies Stream with the stream-term identifying the current input stream.

Errors

Stream is neither a variable nor a stream    domain_error(stream, Stream)

Portability

ISO predicate.

6.10.3 current_output/1

Templates

current_output(?stream)
Description

current_output(Stream) unifies Stream with the stream-term identifying the current output stream.

Errors

Stream is neither a variable nor a stream    domain_error(stream, Stream)

Portability

ISO predicate.

6.10.4 set_input/1

Templates

set_input(+stream_or_alias)
Description

set_input(SorA) sets the current input stream to be the stream associated with the stream-term or alias SorA.

Errors

SorA is a variable    instantiation_error
SorA is neither a variable nor a stream-term or alias    domain_error(stream_or_alias, SorA)
SorA is not associated with an open stream    existence_error(stream, SorA)
SorA is an output stream    permission_error(input, stream, SorA)

Portability

ISO predicate.

6.10.5 set_output/1

Templates

set_output(+stream_or_alias)
Description

set_output(SorA) sets the current output stream to be the stream associated with the stream-term or alias SorA.

Errors

SorA is a variable    instantiation_error
SorA is neither a variable nor a stream-term or alias    domain_error(stream_or_alias, SorA)
SorA is not associated with an open stream    existence_error(stream, SorA)
SorA is an input stream    permission_error(output, stream, SorA)

Portability

ISO predicate.

6.10.6 open/4, open/3

Templates

open(+source_sink, +io_mode, -stream, +stream_option_list)
open(+source_sink, +io_mode, -stream)
Description

open(SourceSink, Mode, Stream, Options) opens the source/sink SourceSink for input or output as indicated by Mode and the list of stream-options Options and unifies Stream with the stream-term which is associated to this stream. See absolute_file_name/2 for information about the syntax of SourceSink (section 6.26.1).

Input/output modes: Mode is an atom which defines the input/output operations that may be performed the stream. Possible modes are:

Stream options: Options is a list of stream options. If this list contains contradictory options, the rightmost option is the one which applies. Possible options are:

open(SourceSink, Mode, Stream, Options) is equivalent to open(SourceSink, Mode, Stream, []).

Errors

SourceSink is a variable    instantiation_error
Mode is a variable    instantiation_error
Options is a partial list or a list with an element E which is a variable    instantiation_error
Mode is neither a variable nor an atom    type_error(atom, Mode)
Options is neither a partial list nor a list    type_error(list, Options)
Stream is not a variable    type_error(variable, Stream)
SourceSink is neither a variable nor a source/sink    domain_error(source_sink, SourceSink)
Mode is an atom but not an input/output mode    domain_error(io_mode, Mode)
an element E of the Options list is neither a variable nor a stream-option    domain_error(stream_option, E)
the source/sink specified by SourceSink does not exist    existence_error(source_sink, SourceSink)
the source/sink specified by SourceSink cannot be opened    permission_error(open, source_sink, SourceSink)
an element E of the Options list is alias(A) and A is already associated with an open stream    permission_error(open, source_sink, alias(A))
an element E of the Options list is reposition(true) and it is not possible to reposition this stream    permission_error(open, source_sink, reposition(true))

Portability

ISO predicates. The buffering/1 stream option is a GNU Prolog extension.

6.10.7 close/2, close/1

Templates

close(+stream_or_alias, +close_option_list)
close(+stream_or_alias)
Description

close(SorA, Options) closes the stream associated with the stream-term or alias SorA. If SorA is the standard input stream or the standard output stream close/2 simply succeeds else the associated source/sink is physically closed. If SorA is the current input stream the current input stream becomes the standard input stream user_input. If SorA is the current output stream the current output stream becomes the standard output stream user_output.

Close options: Options is a list of close options. For the moment only one option is available:

close(SorA) is equivalent to close(SorA, []).

Errors

SorA is a variable    instantiation_error
Options is a partial list or a list with an element E which is a variable    instantiation_error
Options is neither a partial list nor a list    type_error(list, Options)
SorA is neither a variable nor a stream-term or alias    domain_error(stream_or_alias, SorA)
an element E of the Options list is neither a variable nor a close-option    domain_error(close_option, E)
SorA is not associated with an open stream    existence_error(stream, SorA)
SorA needs a special close (section 6.11)    system_error(needs_special_close)

Portability

ISO predicates. The system_error(needs_special_close) is a GNU Prolog extension.

6.10.8 flush_output/1, flush_output/0

Templates

flush_output(+stream_or_alias)
flush_output
Description

flush_output(SorA) sends any buffered output characters/bytes to the stream.

flush_output/0 applies to the current output stream.

Errors

SorA is a variable    instantiation_error
SorA is neither a variable nor a stream-term or alias    domain_error(stream_or_alias, SorA)
SorA is not associated with an open stream    existence_error(stream, SorA)
SorA is an input stream    permission_error(output, stream, SorA)

Portability

ISO predicates.

6.10.9 current_stream/1

Templates

current_stream(?stream)
Description

current_stream(Stream) succeeds if there exists a stream-term that unifies with Stream. This predicate is re-executable on backtracking.

Errors

Stream is neither a variable nor a stream-term    domain_error(stream, Stream)

Portability

GNU Prolog predicate.

6.10.10 stream_property/2

Templates

stream_property(?stream, ?stream_property)
Description

stream_property(Stream, Property) succeeds if current_stream(Stream) succeeds (section 6.10.9) and if Property unifies with one of the properties of the stream. This predicate is re-executable on backtracking.

Stream properties:

Errors

Stream is a variable    instantiation_error
Stream is neither a variable nor a stream-term    domain_error(stream, Stream)
Property is neither a variable nor a stream property    domain_error(stream_property, Property)
Property = file_name(E), mode(E), alias(E), end_of_stream(E), eof_action(E), reposition(E), type(E) or buffering(E) and E is neither a variable nor an atom    type_error(atom, E)

Portability

ISO predicate. The buffering/1 property is a GNU Prolog extension.

6.10.11 at_end_of_stream/1, at_end_of_stream/0

Templates

at_end_of_stream(+stream_or_alias)
at_end_of_stream
Description

at_end_of_stream(SorA) succeeds if the stream associated with stream-term or alias SorA has a stream position end-of-stream or past-end-of-stream. This predicate can be defined using stream_property/2 (section 6.10.10).

at_end_of_stream/0 applies to the current input stream.

Errors

SorA is a variable    instantiation_error
SorA is neither a variable nor a stream-term or alias    domain_error(stream_or_alias, SorA)
SorA is not associated with an open stream    existence_error(stream, SorA)
SorA is an output stream    permission_error(input, stream, SorA)

Portability

ISO predicates. The permission_error(input, stream, SorA) is a GNU Prolog extension.

6.10.12 stream_position/2

Templates

stream_position(+stream_or_alias, ?stream_position)
Description

stream_position(SorA, Position) succeeds unifying Position with the stream-position term associated to the current position of the stream-term or alias SorA. This predicate can be defined using stream_property/2 (section 6.10.10).

Errors

SorA is a variable    instantiation_error
SorA is neither a variable nor a stream-term or alias    domain_error(stream_or_alias, SorA)
Position is neither a variable nor a stream-position term    domain_error(stream_position, Position)
SorA is not associated with an open stream    existence_error(stream, SorA)

Portability

GNU Prolog predicate.

6.10.13 set_stream_position/2

Templates

set_stream_position(+stream_or_alias, +stream_position)
Description

set_stream_position(SorA, Position) sets the position of the stream associated with the stream-term or alias SorA to Position. Position should have previously been returned by stream_property/2 (section 6.10.10) or by stream_position/2 (section 6.10.12).

Errors

SorA is a variable    instantiation_error
Position is a variable    instantiation_error
SorA is neither a variable nor a stream-term or alias    domain_error(stream_or_alias, SorA)
Position is neither a variable nor a stream-position term    domain_error(stream_position, Position)
SorA is not associated with an open stream    existence_error(stream, SorA)
SorA has stream property reposition(false)    permission_error(reposition, stream, SorA)

Portability

ISO predicate.

6.10.14 seek/4

Templates

seek(+stream_or_alias, +stream_seek_method, +integer, ?integer)
Description

seek(SorA, Whence, Offset, NewOffset) sets the position of the stream associated with the stream-term or alias SorA to Offset according to Whence and unifies NewOffset with the new offset from the beginning of the file. seek/4 can only be used on binary streams. Whence is an atom from:

This predicate is an interface to the C Unix function lseek(2).

Errors

SorA is a variable    instantiation_error
Whence is a variable    instantiation_error
Offset is a variable    instantiation_error
SorA is neither a variable nor a stream-term or alias    domain_error(stream_or_alias, SorA)
Whence is neither a variable nor an atom    type_error(atom, Whence)
Whence is an atom but not a valid stream seek method    domain_error(stream_seek_method, Whence)
Offset is neither a variable nor an integer    type_error(integer, Offset)
NewOffset is neither a variable nor an integer    type_error(integer, NewOffset)
SorA is not associated with an open stream    existence_error(stream, SorA)
SorA has stream property reposition(false)    permission_error(reposition, stream, SorA)
SorA is associated with a text stream    permission_error(reposition, text_stream, SorA)

Portability

GNU Prolog predicate.

6.10.15 character_count/2

Templates

character_count(+stream_or_alias, ?integer)
Description

character_count(SorA, Count) unifies Count with the number of characters/bytes read/written on the stream associated with stream-term or alias SorA.

Errors

SorA is a variable    instantiation_error
Count is neither a variable nor an integer    type_error(integer, Count)
SorA is neither a variable nor a stream-term or alias    domain_error(stream_or_alias, SorA)
SorA is not associated with an open stream    existence_error(stream, SorA)

Portability

GNU Prolog predicate.

6.10.16 line_count/2

Templates

line_count(+stream_or_alias, ?integer)
Description

line_count(SorA, Count) unifies Count with the number of lines read/written on the stream associated with the stream-term or alias SorA. This predicate can only be used on text streams.

Errors

SorA is a variable    instantiation_error
Count is neither a variable nor an integer    type_error(integer, Count)
SorA is neither a variable nor a stream-term or alias    domain_error(stream_or_alias, SorA)
SorA is not associated with an open stream    existence_error(stream, SorA)
SorA is associated with a binary stream    permission_error(access, binary_stream, SorA)

Portability

GNU Prolog predicate.

6.10.17 line_position/2

Templates

line_position(+stream_or_alias, ?integer)
Description

line_position(SorA, Count) unifies Count with the number of characters read/written on the current line of the stream associated with the stream-term or alias SorA. This predicate can only be used on text streams.

Errors

SorA is a variable    instantiation_error
Count is neither a variable nor an integer    type_error(integer, Count)
SorA is neither a variable nor a stream-term or alias    domain_error(stream_or_alias, SorA)
SorA is not associated with an open stream    existence_error(stream, SorA)
SorA is associated with a binary stream    permission_error(access, binary_stream, SorA)

Portability

GNU Prolog predicate.

6.10.18 stream_line_column/3

Templates

stream_line_column(+stream_or_alias, ?integer, ?integer)
Description

stream_line_column(SorA, Line, Column) unifies Line (resp. Column) with the current line number (resp. column number) of the stream associated with the stream-term or alias SorA. This predicate can only be used on text streams. Note that Line corresponds to the value returned by line_count/2 + 1 (section 6.10.16) and Column to the value returned by line_position/2 + 1 (section 6.10.17).

Errors

SorA is a variable    instantiation_error
Line is neither a variable nor an integer    type_error(integer, Line)
Column is neither a variable nor an integer    type_error(integer, Column)
SorA is neither a variable nor a stream-term or alias    domain_error(stream_or_alias, SorA)
SorA is not associated with an open stream    existence_error(stream, SorA)
SorA is associated with a binary stream    permission_error(access, binary_stream, SorA)

Portability

GNU Prolog predicate.

6.10.19 set_stream_line_column/3

Templates

set_stream_line_column(+stream_or_alias, +integer, +integer)
Description

set_stream_line_column(SorA, Line, Column) sets the stream position of the stream associated with the stream-term or alias SorA according to the line number Line and the column number Column. This predicate can only be used on text streams. It first repositions the stream to the beginning of the file and then reads character by character until the required position is reached.

Errors

SorA is a variable    instantiation_error
Line is a variable    instantiation_error
Column is a variable    instantiation_error
Line is neither a variable nor an integer    type_error(integer, Line)
Column is neither a variable nor an integer    type_error(integer, Column)
SorA is neither a variable nor a stream-term or alias    domain_error(stream_or_alias, SorA)
SorA is not associated with an open stream    existence_error(stream, SorA)
SorA is associated with a binary stream    permission_error(reposition, binary_stream, SorA)
SorA has stream property reposition(false)    permission_error(reposition, stream, SorA)

Portability

GNU Prolog predicate.

6.10.20 add_stream_alias/2

Templates

add_stream_alias(+stream_or_alias, +atom)
Description

add_stream_alias(SorA, Alias) adds Alias as a new alias to the stream associated with the stream-term or alias SorA.

Errors

SorA is a variable    instantiation_error
Alias is a variable    instantiation_error
Alias is neither a variable nor an atom    type_error(atom, Alias)
SorA is neither a variable nor a stream-term or alias    domain_error(stream_or_alias, SorA)
SorA is not associated with an open stream    existence_error(stream, SorA)
Alias is already associated with an open stream    permission_error(add_alias, source_sink, alias(Alias))

Portability

GNU Prolog predicate.

6.10.21 current_alias/2

Templates

current_alias(?stream, ?atom)
Description

current_alias(Stream, Alias) succeeds if current_stream(Stream) succeeds (section 6.10.9) and if Alias unifies with one of the aliases of the stream. It can be defined using stream_property/2 (section 6.10.10). This predicate is re-executable on backtracking.

Errors

Stream is neither a variable nor a stream-term    domain_error(stream, Stream)
Alias is neither a variable nor an atom    type_error(atom, Alias)

Portability

GNU Prolog predicate.

6.10.22 set_stream_type/2

Templates

set_stream_type(+stream_or_alias, +atom)
Description

set_stream_type(SorA, Type) updates the type associated with stream-term or alias SorA. The value of Type is an atom in text or binary as for open/4 (section 6.10.6). The type of a stream can only be changed before any input/output operation is executed.

Errors

SorA is a variable    instantiation_error
Type is a variable    instantiation_error
Type is neither a variable nor a valid type    domain_error(stream_type, Type)
SorA is neither a variable nor a stream-term or alias    domain_error(stream_or_alias, SorA)
SorA is not associated with an open stream    existence_error(stream, SorA)
An I/O operation has already been executed on SorA    permission_error(modify, stream, SorA)

Portability

GNU Prolog predicate.

6.10.23 set_stream_eof_action/2

Templates

set_stream_eof_action(+stream_or_alias, +atom)
Description

set_stream_eof_action(SorA, Action) updates the eof_action option associated with the stream-term or alias SorA. The value of Action is one of the atoms error, eof_code, reset as for open/4 (section 6.10.6).

Errors

SorA is a variable    instantiation_error
Action is a variable    instantiation_error
Action is neither a variable nor a valid eof action    domain_error(eof_action, Action)
SorA is neither a variable nor a stream-term or alias    domain_error(stream_or_alias, SorA)
SorA is not associated with an open stream    existence_error(stream, SorA)
SorA is an output stream    permission_error(modify, stream, SorA)

Portability

GNU Prolog predicate.

6.10.24 set_stream_buffering/2

Templates

set_stream_buffering(+stream_or_alias, +atom)
Description

set_stream_buffering(SorA, Buffering) updates the buffering mode associated with the stream-term or alias SorA. The value of Buffering is one of the atoms none, line or block as for open/4 (section 6.10.6).

Errors

SorA is a variable    instantiation_error
Buffering is a variable    instantiation_error
Buffering is neither a variable nor a valid buffering mode    domain_error(buffering_mode, Buffering)
SorA is neither a variable nor a stream-term or alias    domain_error(stream_or_alias, SorA)
SorA is not associated with an open stream    existence_error(stream, SorA)

Portability

GNU Prolog predicate.


Copyright (C) 1999,2000 Daniel Diaz

Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved.

More about the copyright
Previous Contents Next