[ Term I/O | The ECLiPSe Built-In Predicates | Reference Manual | Alphabetic Index ]

read(+Stream, -Term)

Succeeds if the next term from the input stream Stream is successfully read and unified with Term.
Stream
Integer (stream number) or Atom (reserved or user-defined symbolic stream name).
Term
Prolog term or variable.

Description

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 atom end_of_file.

The default action for syntax errors is to print a warning and fail.

Modes and Determinism

Modules

This predicate is sensitive to its module context (tool predicate, see @/2).

Fail Conditions

Fails if a syntax error was detected and no term could be read

Exceptions

(4) instantiation fault
Stream is not instantiated.
(5) type error
Stream is not an atom or an integer.
(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.

Examples

Success:
      [eclipse]: read(0,Term).
      > atom.
      Term = atom
      yes.

      [eclipse]: 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).').
      yes.
      [eclipse]: system('cat file1').
      f(1,2,3).
      g(1,2,3). h(1,2,3).
      i.
      j(1, 2
      ,3).
      yes.
      [eclipse]: 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
      yes.
Fail:
      [eclipse]: read(0,a).
      > b.
      no.

      [eclipse]: read(0,X).
      > f(1,2)m.
              ^ (here?)
      syntax error: postfix/infix operator expected
      no (more) solution.
Error:
      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).



See Also

read / 1, readvar / 3, read_token / 2, read_token / 3