Next: Output Port Operations, Previous: Constructors and Accessors for Ports, Up: Port Primitives
This section describes the standard operations on input ports. Following that, some useful custom operations are described.
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.
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
.
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.
These operations are like
read-char
, except that they read or discard multiple characters at once. 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, whilediscard-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.
Reads characters from input-port into the substring defined by string, start, and end until either the substring has been filled or there are no more characters available. Returns the number of characters written to the substring.
If input-port is an interactive port, and at least one character is immediately available, the available characters are written to the substring and this operation returns immediately. If no characters are available, and input-port is in blocking mode, the operation blocks until at least one character is available. Otherwise, the operation returns
#f
immediately.This is an extremely fast way to read characters from a port.
Each of these procedures invokes the respective operation on input-port. For example, the following are equivalent:
(input-port/read-char input-port) ((port/operation input-port 'read-char) input-port)
The following custom operations are implemented for input ports to files, and will also work with some other kinds of input ports:
Returns
#t
if input-port is known to be at end of file, otherwise it returns#f
.
Returns 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
.
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.