next up previous index
Next: Communication with Streams Up: Input and Output Previous: System Streams   Index


Opening New Streams

A stream is 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:

Note that path names usually have to be quoted (in single or double quotes) because they contain non-alphanumeric characters.

If SourceSink is of the form string(InitString) a pseudo-file in memory is opened, see section 12.5.1.

If SourceSink is of the form queue(InitString) a pseudo-pipe in memory is opened, see section 12.5.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 logical stream identifier or an uninstantiated variable. If it is uninstantiated, the system will create an identifier. This stream identifier 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)
will write the atom subject to the file `foo'. A stream Stream opened by the open/3 predicate may be subsequently closed by the call
close(Stream)

The predicate pipe/2 is used like

pipe(In, Out)
and opens a pipe, i.e. two streams, In for reading and Out for writing, which are connected together using the pipe(2) system call. This mechanism is normally used to communicate with other processes which were forked by the main process.

Sockets streams are opened with the primitives socket/3 and accept/3, more details are in chapter 21.

On most devices, output is buffered, i.e. any output does not appear immediately on the file, pipe or socket, but goes into a buffer first. To make sure the data is actually written to the device, the stream usually has to be flushed using flush/1. If this is forgotten, the receiving end of a pipe or socket may hang in a blocking read operation.


next up previous index
Next: Communication with Streams Up: Input and Output Previous: System Streams   Index

1999-08-06