projog

3.72. get_code(X) - reads the next character from the input stream.

The goal succeeds if X can be unified with next character read from the current input stream. Succeeds only once and the operation of moving to the next character is not undone on backtracking.

If there are no more characters to read from the current input stream (i.e. if the end of the stream has been reached) then an attempt is made to unify X with the value -1.

Examples

write_to_file(X) :-
open('get_code.tmp', write, Z),
set_output(Z),
writef(X),
close(Z),
set_output('user_output').

read_from_file :-
open('get_code.tmp', read, Z),
set_input(Z),
print_contents,
close(Z).

print_contents :-
repeat,
get_code(C),
write(C),
nl,
C =:= -1,
!.

?- write_to_file('ab\tc\r\nxyz').

yes

?- read_from_file.
97
98
9
99
13
10
120
121
122
-1

yes

force_error :-
open('get_code.tmp', read, Z),
set_input(Z),
close(Z),
print_contents.

?- force_error.

Could not read next character from input streamSee Input and Output