Moreover, every ECLiPSe session defines the following symbolic stream names, which are used for certain categories of input/output:
Symbolic Stream | System Stream |
input | 0 (stdin) |
output | 1 (stdout) |
warning_output | 1 (stdout) |
log_output | 1 (stdout) |
error | 2 (stderr) |
3 (null) |
An alias name can be given to a stream either when it is created or explicitly by invoking set_stream/2:
set_stream(Alias, Stream)To find the corresponding stream number, use get_stream/2:
get_stream(StreamOrAlias, StreamNr)get_stream/2 can also be used to check whether two stream names are aliases of each other.
Streams provide a uniform interface to a variety of I/O devices and pseudo-devices. The following table gives an overview of how streams on the different devices are opened.
I/O device | How to open |
tty | implicit (stdin,stdout,stderr) or open/3 of a device file |
file | open(FileName, Mode, Stream) |
string | open(string(String), Mode, Stream) |
queue | open(queue(String), Mode, Stream) |
pipe | exec/2, exec/3 and exec_group/3 |
socket | socket/3 and accept/3 |
null | implicit (null stream) |
Most streams are opened for input or output by means of the open/3 or open/4 predicate. The goals
open(SourceSink, Mode, Stream) open(SourceSink, Mode, Stream, Options)open a communication channel with SourceSink.
If SourceSink is an atom or a string, a file is being opened and SourceSink takes the form of a file name in the host machine environment. ECLiPSe uses an operating system independent path name syntax, where the components are separated by forward slashes. The following forms are possible:
~
/prolog/file.pl
~
peter/prolog/file.pl
(UNIX only)
If SourceSink is of the form string(InitString) a pseudo-file in memory is opened, see section 10.3.1.
If SourceSink is of the form queue(InitString) a pseudo-pipe in memory is opened, see section 10.3.2.
Mode must be one of the atoms read, write, append or update, which means that the stream is to be opened for input, output, output at the end of the existing stream, or both input and output, respectively. Opening a file in write mode will create it if it does not exist, and erase the previous contents if it does exist. Opening a file in append mode will keep the current contents of the file and start writing at its end.
Stream is a symbolic stream identifier or an uninstantiated variable. If it is uninstantiated, the system will bind it to an identifier (the stream number):
[eclipse 1]: open(new_file, write, Stream). Stream = 6 yes.If the stream argument is an atomic name, this name becomes an alias for the (hidden) stream number:
[eclipse 1]: open(new_file, write, new_stream). yes.The stream identifier (symbolic or numeric) may then be used in predicates which have a named stream as one of their arguments. For example
open("foo", update, Stream), write(Stream, subject), close(Stream).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.
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.
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.
current_stream(?Stream)can be used to backtrack over all the currently opened stream indentifiers (but not their aliases).
get_stream_info(+Stream, +Property, -Value)e.g. its mode, line number, file name etc. Some stream properties can be modified using set_stream_property/3
set_stream_property(+Stream, +Property, +Value)e.g. the end-of-line sequence used, the flushing behaviour, the event-raising behaviour, the prompt etc.