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

Opening New Streams

  A stream is opened for input or output by means of the open/3 predicate.   The goal

open(File, Mode, Stream)
opens a communication channel with the file specified by File.

File may be an atom or a string, and takes the form of a file name in the host machine environmentgif. In the UNIX environment, file names can take several forms:

Mode is one of the atoms read, write, append, update or string, where the stream is to be opened for input, output, output at the end of the existing file, both input and output or to a string, 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. The mode string specifies a stream to/from a string and is described in detail in section gif.

Stream is a logical stream identifier or an uninstantiated variable. The 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(In, Out)
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, but it can also be used to temporarily store some data instead of writing it into a file, however for this purpose a string stream is more efficient. Before any reading is possible, the output must be usually flushed with flush/1. The term being read from the input end of the pipe must be written in a valid Prolog syntax, and so it must be terminated by a fullstop. An example of its use:
[eclipse 1]: pipe(in, out).

yes.
[eclipse 2]: write(out, "output_term(args).\n"), flush(out).

yes.
[eclipse 3]: read(in, X).

X = output_term(args)
yes.
If too much data is written into the pipe without reading it, ECLiPSe may be suspended by the operating system.



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



Micha Meier
Mon Mar 4 12:11:45 MET 1996