This built-in predicate reads a single term from the specified input stream, the stream is altered. The input stream must be a text stream that was opened for reading.
The Prolog flag console_newline may affect read/2.
see also: char_conversion/2 current_char_conversion/2 current_op/3 op/3 read/1 read_term/2 read_term/3 write/1 write/2 write_canonical/1 write_canonical/2 write_list/1 write_list/2 write_list_goal/2 write_list_goal/3 write_term/2 write_term/3 writeq/1 writeq/2
| Example | |
| read(user_in, A). | assume the input stream 'user_in' has contents: foo(B, C)).
test(1). ... A is assigned the value foo(__1, __2) and the stream is left as: test1(1). ... |
| read(user_in, test(O)). | assume the current input stream has contents: test([a,b]).
test2(2) ... O is assigned the value [a,b] and read/2 succeeds, and the stream is left as: test2(2) ... |
| Exceptions | |
| stream_or_alias is a variable | an instantiation_error exception is thrown |
| stream_or_alias is neither a variable nor a stream term nor an alias | a domain_error(stream_or_alias, IS) exception is thrown, the variable IS is assigned the incorrect stream_or_alias term |
| stream_or_alias is not associated with an open stream | an existence_error(stream, IS) exception is thrown |
| stream_or_alias is an output stream | a permission_error(input, stream, IS) exception is thrown |
| stream_or_alias is a binary stream | a permission_error(input, binary_stream, IS) is thrown, the variable IS is assigned the stream which was used as the input stream |
| the current input stream IS is associated with a binary stream | a permission_error(input, binary_stream, IS) is thrown and IS is substituted by the binary stream |
| the current input stream IS has stream properties end_of_stream(past) and eof_action(error) | a permission_error(input, past_end_of_stream, IS) is thrown and IS is substituted by the stream which was used for reading the term |
| the sequence of tokens cannot be parsed as a term using the current operator table | a syntax_error(A) exception is thrown, the variable A is replaced by an error message of Trinc-Prolog which describes the error |