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 an atom with the value end_of_file
.
Examples
write_to_file(X) :-
open('get_char_test.tmp', write, Z),
set_output(Z),
writef(X),
close(Z),
set_output('user_output').
read_from_file :-
open('get_char_test.tmp', read, Z),
set_input(Z),
print_contents,
close(Z).
print_contents :-
repeat,
get_char(C),
write(C),
nl,
C=='end_of_file',
!.
?- write_to_file('abc\nxyz').
yes
?- read_from_file.
a
b
c
x
y
z
end_of_file
yes
force_error :-
open('get_char_test.tmp', read, Z),
set_input(Z),
close(Z),
print_contents.
?- force_error.
Could not read next character from input streamSee Input and Output