read(X)which reads the next term in the current input stream, which means the window on your workstation unless you have done something slightly fancy with files, and unifies it with the variable
end_of_file: if there is nothing to read in the current input stream,
Xto be bound to the special symbol
end_of_file. Unless you are absolutely sure you have some other way of knowing when there will be no more input data to read, you should check to make sure that the term that you have read is not
end_of_file. Ways that you might be (almost) absolutely sure: the first term read might be a number indicating how many further terms are to be read.
get_byte(C)which reads a single character from the current input stream, and binds the equivalent integer code, in the range 0 to 255. If there is no further character to read,
Cis bound to
get_byte(C), except that it reads the first non-blank character, if any, from the current input stream.
flush_outputis actually an output goal, which ensures that any output which has been requested but not yet performed, is performed right now. Output might not be completed until there is enough to make it worthwhile, or until an operation like
Example 1: Assume that input is coming from the user's workstation
window and that procedure
read_a_char is defined as:
read_a_char(C) :- write('Type: '), flush_output, get_byte(C).
Then you can do the following - note that 43 is the character
code for the character
?- read_a_char(Ch). Type: + Ch = 43
for conversion of a string of numeric character
codes to an atom composed of the characters represented by those
character codes. For example, 102, 105, 100, and 111 are the
numeric codes for the letters f, i, d, and o. atom_codes
can be used as follows:
?- atom_codes(A, [102, 105, 100, 111]). A = fido
Example 2: Assume that there is a file called
which contains on its first line the term
with a full stop at the end of the term.
?- see('inputdata'), read(Term), seen. Term = likes(mary, pizza)
NB: No full stop at the end of Term's binding.