[ Term I/O | Reference Manual | Alphabetic Index ]
Succeeds if the next term from the input stream Stream is successfully read
and unified with Term.
- Stream handle or alias (atom)
- Prolog term or variable.
Used to read the next term from the input stream Stream and unify it
with Term. If there is more than one Prolog term in the file, the term
must be in Prolog term format i.e. terminated by fullstop (a period and
a blank space character), neither of which are retained by Prolog.
Otherwise, end of file acts like fullstop. If only end of file is read,
the event 190 is raised and the default handler unifies Term with the
The default action for syntax errors is to print a warning and fail.
Modes and Determinism
This predicate is sensitive to its module context (tool predicate, see @/2).
Fails if a syntax error was detected and no term could be read
- (4) instantiation fault
- Stream is not instantiated.
- (5) type error
- Stream is not an atom or a stream handle.
- (190) end of file reached
- End of file was encountered before reading any character.
- (192) illegal stream mode
- Stream is not an input stream.
- (193) illegal stream specification
- Stream is an illegal stream specification.
- (198) reading past the file end
- Trying to read even after the error 190 was raised.
Term = atom
?- open(file1,write,s),write(s, 'f(1,2,3).\ng(1,2'),
write(s, ',3). h(1,2,3).\ni.\nj(1, 2\n,3).').
?- system('cat file1').
?- open(file1,read,s), read(s,A), read(s,B),
read(s,C), read(s,D), read(s,E), read(s,F).
A = f(1, 2, 3)
B = g(1, 2, 3)
C = h(1, 2, 3)
D = i
E = j(1, 2, 3)
F = end_of_file
syntax error: postfix/infix operator expected
no (more) solution.
read(a(b,c),S). (Error 4).
read("string", a(b,c)). (Error 5).
read(9, X=2). (Error 192). % stream not open
read(atom, X=2). (Error 193).
read / 1, read_term / 2, read_token / 2, read_token / 3