Home Contents Index Summary Previous Next

4.18 Primitive character I/O

See section 4.2 for an overview of supported character representations.

nl
Write a newline character to the current output stream. On Unix systems nl/0 is equivalent to put(10).

nl(+Stream)
Write a newline to Stream.

put(+Char)
Write Char to the current output stream, Char is either an integer-expression evaluating to a character code (0 =<Char =< 255) or an atom of one character.

put(+Stream, +Char)
Write Char to Stream.

put_byte(+Byte)
Alias for put/1.

put_byte(+Stream, +Byte)
Alias for put/2

put_char(+Char)
Alias for put/1.

put_char(+Stream, +Char)
Alias for put/2

put_code(+Code)
Alias for put/1.

put_code(+Stream, +Code)
Alias for put/2

tab(+Amount)
Writes Amount spaces on the current output stream. Amount should be an expression that evaluates to a positive integer (see section 4.26).

tab(+Stream, +Amount)
Writes Amount spaces to Stream.

flush_output
Flush pending output on current output stream. flush_output/0 is automatically generated by read/1 and derivatives if the current input stream is user and the cursor is not at the left margin.

flush_output(+Stream)
Flush output on the specified stream. The stream must be open for writing.

ttyflush
Flush pending output on stream user. See also flush_output/[0,1].

get_byte(-Byte)
Read the current input stream and unify the next byte with Byte (an integer between 0 and 255. Byte is unified with -1 on end of file.

get_byte(+Stream, -Byte)
Read the next byte from Stream.

get_code(-Code)
Read the current input stream and unify Code with the character code of the next character. Code is unified with -1 on end of file. See also get_char/1.

get_code(+Stream, -Code)
Read the next character-code from Stream.

get_char(-Char)
Read the current input stream and unify Char with the next character as a one-character-atom. See also atom_chars/2. On end-of-file, Char is unified to the atom end_of_file.

get_char(+Stream, -Char)
Unify Char with the next character from Stream as a one-character-atom. See also get_char/2, get_byte/2 and get_code/2.

get0(-Char)
Edinburgh version of the ISO get_byte/1 predicate.

get0(+Stream, -Char)
Edinburgh version of the ISO get_byte/2 predicate.

get(-Char)
Read the current input stream and unify the next non-blank character with Char. Char is unified with -1 on end of file.

get(+Stream, -Char)
Read the next non-blank character from Stream.

peek_byte(-Byte)
Reads the next input byte like get_byte/1, but does not remove it from the input stream.

peek_byte(+Stream, -Byte)
Reads the next input byte like get_byte/2, but does not remove it from the stream.

peek_code(-Code)
Reads the next input code like get_code/1, but does not remove it from the input stream.

peek_code(+Stream, -Code)
Reads the next input code like get_code/2, but does not remove it from the stream.

peek_char(-Char)
Reads the next input character like get_char/1, but does not remove it from the input stream.

peek_char(+Stream, -Char)
Reads the next input character like get_char/2, but does not remove it from the stream.

skip(+Char)
Read the input until Char or the end of the file is encountered. A subsequent call to get0/1 will read the first character after Char.

skip(+Stream, +Char)
Skip input (as skip/1) on Stream.

get_single_char(-Char)
Get a single character from input stream `user' (regardless of the current input stream). Unlike get0/1 this predicate does not wait for a return. The character is not echoed to the user's terminal. This predicate is meant for keyboard menu selection etc. If SWI-Prolog was started with the -tty option this predicate reads an entire line of input and returns the first non-blank character on this line, or the character code of the newline (10) if the entire line consisted of blank characters.

at_end_of_stream
Succeeds after the last character of the current input stream has been read. Also succeeds if there is no valid current input stream.

at_end_of_stream(+Stream)
Succeeds after the last character of the named stream is read, or Stream is not a valid input stream. The end-of-stream test is only available on buffered input stream (unbuffered input streams are rarely used, see open/4).

copy_stream_data(+StreamIn, +StreamOut, +Len)
Copy Len bytes from stream StreamIn to StreamOut.

copy_stream_data(+StreamIn, +StreamOut)
Copy data all (remaining) data from stream StreamIn to StreamOut.

read_pending_input(+StreamIn, -Codes, ?Tail)
Read input pending in the input buffer of StreamIn and return it in the difference list Codes-Tail. I.e. the available characters codes are used to create the list Codes ending in the tail Tail. This predicate is intended for efficient unbuffered copying and filtering of input comming from network connections or devices.

The following code fragment realises effient non-blocking copy of data from an input- to an output stream. The at_end_of_stream/1 call checks for end-of-stream and fills the input buffer. Note that the use of a get_code/2 and put_code/2 based loop requires a flush_output/1 call after each put_code/2. The copy_stream_data/2 does not allow for inspection of the copied data and suffers from the same buffering issues.


copy(In, Out) :-
        repeat,
            (   at_end_of_stream(In)
            ->  !
            ;   read_pending_input(In, Chars, []),
                format(Out, '~s', [Chars]),
                flush_output(Out),
                fail
            ).