Download pdf format

Transcript
will write the atom subject to the file ‘foo’ and close the stream subsequently.
It is recommended style not to use symbolic stream names in code that is meant to be reused.
This is because the stream names are global, there is the possibility of name clashes, and the
code will not be reentrant. It is cleaner to open streams with a variable for the stream identifier
and pass the identifier as an argument wherever it is needed.
Socket streams are not opened with open/3, but with the special primitives socket/3 and
accept/3. More details are in chapter 21.
A further group of primitives which open streams implicitly is exec/2, exec/3 and and exec group/3.
They open pipes which connect directly to the I/O channels of the executed process. See chapter
20 for details.
10.1.4
Closing Streams
The predicate
close(Stream)
is used to close an open stream. If a stream has several alias names, closing any of them will
close the actual stream. All the other aliases should be closed as well (or redirected to streams
that are still open), because otherwise they will continue to refer to the number of the already
closed stream.
When an attempt is made to close a redirected system stream (e.g. output), the stream is closed,
but the system stream is reset to its default setting.
10.1.5
Redirecting Streams
The set stream/2 primitive can be used to redirect an already existing symbolic stream to a
new actual stream. This is particularly useful to redirect e.g. the default output stream
set_stream(output, MyStream)
so that all standard output is redirected to some other destination (e.g. an opened file instead of
the terminal). Note that the stream modes (read/write) must be compatible. The redirection
is terminated by calling
close(output)
which will reestablish the original meaning of the output stream.
10.1.6
Finding Streams
The predicate
current_stream(?Stream)
can be used to backtrack over all the currently opened stream indentifiers (but not their aliases).
80