Go to the previous, next section.

Input Port Operations

This section describes the standard operations on input ports. Following that, some useful custom operations are described.

operation+: input port read-char input-port

Removes the next character available from input-port and returns it. If input-port has no more characters and will never have any (e.g. at the end of an input file), this operation returns an end-of-file object. If input-port has no more characters but will eventually have some more (e.g. a terminal where nothing has been typed recently), and it is in non-blocking mode, #f is returned; otherwise the operation hangs until input is available.

operation+: input port peek-char input-port

Reads the next character available from input-port and returns it. The character is not removed from input-port, and a subsequent attempt to read from the port will get that character again. In other respects this operation behaves like read-char.

operation+: input port discard-char input-port

Discards the next character available from input-port and returns an unspecified value. In other respects this operation behaves like read-char.

operation+: input port char-ready? input-port k

char-ready? returns #t if at least one character is available to be read from input-port. If no characters are available, the operation waits up to k milliseconds before returning #f, returning immediately if any characters become available while it is waiting.

operation+: input port read-string input-port char-set

operation+: input port discard-chars input-port char-set

These operations are like read-char and discard-char, except that they read or discard multiple characters at once. This can have a marked performance improvement on buffered input ports. All characters up to, but excluding, the first character in char-set (or end of file) are read from input-port. read-string returns these characters as a newly allocated string, while discard-chars discards them and returns an unspecified value. These operations hang until sufficient input is available, even if input-port is in non-blocking mode. If end of file is encountered before any input characters, read-string returns an end-of-file object.

procedure+: input-port/operation/read-char input-port

procedure+: input-port/operation/peek-char input-port

procedure+: input-port/operation/discard-char input-port

procedure+: input-port/operation/char-ready? input-port

procedure+: input-port/operation/read-string input-port

procedure+: input-port/operation/discard-chars input-port

Each of these procedures returns the procedure that implements the respective operation for input-port. Each is equivalent to, but faster than, input-port/operation on the respective operation name:

(input-port/operation/read-char input-port)
(input-port/operation input-port 'read-char)

procedure+: input-port/read-char input-port

procedure+: input-port/peek-char input-port

procedure+: input-port/discard-char input-port

procedure+: input-port/char-ready? input-port k

procedure+: input-port/read-string input-port char-set

procedure+: input-port/discard-chars input-port char-set

Each of these procedures invokes the respective operation on input-port. For example, the following are equivalent:

(input-port/read-string input-port char-set)
((input-port/operation/read-string input-port) input-port char-set)

The following custom operations are implemented for input ports to files, and will also work with some other kinds of input ports:

operation+: input port eof? input-port

Returns #t if input-port is known to be at end of file, otherwise it returns #f.

operation+: input port read-chars input-port string

Attempts to read enough characters from input-port to fill string, returning the number of characters actually read. The string will be completely filled unless the port is unable to deliver the characters; this can happen when it is a file port and there aren't that many characters available, or when it is an interactive port in non-blocking mode and doesn't have that many characters immediately available. This is an extremely fast way to read characters from the port.

operation+: input port read-substring input-port string start end

Attempts to read enough characters from input-port to fill the substring specified by string, start, and end, returning the number of characters actually read. The string will be completely filled unless the port is unable to deliver the characters; this can happen when it is a file port and there aren't that many characters available, or when it is an interactive port in non-blocking mode and doesn't have that many characters immediately available. This is an extremely fast way to read characters from the port.

operation+: input port chars-remaining input-port

Computes an estimate of the number of characters remaining to be read from input-port. This is useful only when input-port is a file port in binary mode; in other cases, it returns #f.

operation+: input port buffered-input-chars input-port

Returns the number of unread characters that are stored in input-port's buffer. This will always be less than or equal to the buffer's size.

operation+: input port input-buffer-size input-port

Returns the maximum number of characters that input-port's buffer can hold.

operation+: input port set-input-buffer-size input-port size

Resizes input-port's buffer so that it can hold at most size characters. Characters in the buffer are discarded. Size must be an exact non-negative integer.

Go to the previous, next section.