The read/1, 2 and write/1, 2 predicates correspond to the third way of looking at streams. The goal
reads the next term from the current input stream and unifies it with Term. The input term must be followed by a full stop, that is, a '.' character followed by a layout character (tab, space or newline) or by the end of file. If end of file has been reached then an exception is raised, the default handler causes the atom end_of_file to be returned. A term may be read from a stream other than the current input stream by the callread(Term)
which reads the term from the named stream. The goalread(Stream, Term)
writes Term to the current output stream. This is done by taking the current operator declarations into account. Output produced by the write/1, 2 predicate is not (necessarily) in a form suitable for subsequent input to a Prolog program using the read/1 predicate, for this purpose writeq/1, 2 is to be used. The goalwrite(Term)
writes Term to the named output stream. The predicatewrite(Stream, Term)
outputs the Term on the current output stream in the functor syntax, ignoring possible operator declarations. The predicatedisplay(Term)
can be used to read a term from the specified stream and obtain the list of variable names contained in the Term. VarList is a list of pairs [VarNameVar] where VarName is the atom corresponding to the variable name and Var is the corresponding variable.readvar(Stream, Term, VarList)
When the flag variable_names is switched off, the output predicates are not able to write free variables in their source form, i.e. with the correct variable names. Then the variables are output in the form
where a is a character which depends on the memory area where the variable is located or on its properties: l for a local variable, g for a global variable or a metaterm. N is a number._aN
It is possible to pass any input stream to the ECLiPSe compiler using the predicate
and it is of course possible to mix the compilation with other input predicates. If, for example, the file a.pl contains the following datacompile_stream(Stream)
it is possible to executep(1). p(2). end_of_file. p(3).
To specify a position in the file to write to or read from, the predicate seek/2 is provided. The call[eclipse 1]: open('a.pl', read, a). yes. [eclipse 2]: read(a, X). X = p(1) yes. [eclipse 3]: compile_stream(a). a.pl compiled 40 bytes in 0.00 seconds yes. [eclipse 4]: read(a, X). X = p(3) yes. [eclipse 5]: p(X). X = 2 yes.
moves the current position in the file (the 'file pointer') to the offset Pointer (a number specifying the length in bytes) from the start of the file. If Pointer is the atom end_of_file the current position is moved to the end of the file. Hence a file could be open in append mode usingseek(Stream, Pointer)
The current position in a file may be found by the predicate at/2. The callopen(File, update, Stream), seek(Stream, end_of_file)
unifies Pointer with the current position in the file. The predicateat(Stream, Pointer)
succeeds if the current position in the given stream is at the file end.at_eof(Stream)