Download MIT Scheme Reference Manual

Transcript
Chapter 14: Input/Output
169
write-to-string object [k]
procedure
Writes object to a string output port, and returns the resulting newly allocated string.
If k is supplied and not #f, this procedure is equivalent to
(with-output-to-truncated-string k
(lambda ()
(write object)))
otherwise it is equivalent to
(with-output-to-string
(lambda ()
(write object)))
14.4 Input Procedures
This section describes the procedures that read input. Input procedures can read either
from the current input port or from a given port. Remember that to read from a file, you
must first open a port to the file.
Input ports can be divided into two types, called interactive and non-interactive. Interactive input ports are ports that read input from a source that is time-dependent; for example,
a port that reads input from a terminal or from another program. Non-interactive input
ports read input from a time-independent source, such as an ordinary file or a character
string.
All optional arguments called input-port, if not supplied, default to the current input
port.
read-char [input-port]
procedure
Returns the next character available from input-port, updating input-port to point
to the following character. If no more characters are available, an end-of-file object
is returned.
In MIT Scheme, if input-port is an interactive input port and no characters are
immediately available, read-char will hang waiting for input, even if the port is in
non-blocking mode.
peek-char [input-port]
procedure
Returns the next character available from input-port, without updating input-port to
point to the following character. If no more characters are available, an end-of-file
object is returned.2
In MIT Scheme, if input-port is an interactive input port and no characters are
immediately available, peek-char will hang waiting for input, even if the port is in
non-blocking mode.
2
The value returned by a call to peek-char is the same as the value that would have been
returned by a call to read-char on the same port. The only difference is that the very
next call to read-char or peek-char on that input-port will return the value returned
by the preceding call to peek-char. In particular, a call to peek-char on an interactive
port will hang waiting for input whenever a call to read-char would have hung.