[top] [up] [next]

Input and output


Opening and closing of streams

The standard input and output streams do not have to be opened and cannot be closed. To open a stream the predicate open/3 or open/4 must be used. The open/3 predicate opens a stream with the default options, the default options are: a text stream which cannot be repositioned, has no alias and a permission_error exception is thrown if data is read past the end of the stream. The open options can be specified with the predicate open/4, the possible values for the options are:

type(T) Specifies whether the stream is a text or a binary stream, T will be:
text: the stream is a text stream (default)
binary: the stream is a binary stream
reposition(Bool) Depends on the type of the source/sink if repositioning is possible or not, Bool will be:
true: it is possible to reposition the stream
false: it is not possible to reposition the stream (default)
alias(Name) Name will be an atom used as an alias to denote the stream, only the standard streams have default aliases.
eof_action(Action) The effect of attempting to input from a stream whose position is past-end-of-stream is determined by the value of this parameter:
error: a permission_error exception is thrown (default)
eof_code: the result of input is as if the stream position is end-of-stream and end-of-file code is returned
reset: the stream position is reset to the beginning so that it is not past-end-of-stream and another attempt is made to input from it.

If the stream-options list contains contradictory stream-options, the rightmost option is the one which applies.

  %Open a binary output stream with an alias, it can be repositioned and if
  %the stream is read past the end then the stream is reset
  :- open('c:\\temp\\output.txt', write, StreamTerm, [alias(out), type(binary), reposition(true), eof_action(reset)]).

A stream is a sequence of characters and a text stream can be regarded as a number of lines of text each separated by a carriage return and a linefeed character. On input a carriage-return/linefeed pair of characters is translated into a single linefeed character and on output a single linefeed character is translated to a carriage-return/linefeed pair. Because of the translation of characters it is not advisable to adjust the position of the stream when reading from or writing to a text stream. With binary files no such translation takes place.

There are three possible I/O modes that can be specified when opening a source/sink:

To close a stream there are the predicates close/1 and close/2, with close/2 it is possible to specify a close-option. The close option is force/1.

force(Bool) this option determines what is done if an error condition is detected during closing of the stream, the possible values are:
true: the stream is always closed even in case of an error, this can result in the loss of data if data was buffered
false: the stream is not closed if an error condition is detected (default)

To specify which stream to close an alias or stream-term must be used, below is an example of using an alias to close a stream.

  %Close the stream with the alias 'out'
  :- close(out).

[top] [up] [next]

 

info@trinc-prolog.com