Some operations may be performed on any kind of stream; the next figure provides a list of standardized operations that are potentially useful with any kind of stream.
close | stream-element-type |
input-stream-p | streamp |
interactive-stream-p | with-open-stream |
output-stream-p |
Figure 21–1. Some General-Purpose Stream Operations
Other operations are only meaningful on certain stream types. For example, read-char is only defined for character streams and read-byte is only defined for binary streams.
:direction :probe” is given to open) neither.
The next figure shows operators relating to input streams.
clear-input | read-byte | read-from-string |
listen | read-char | read-line |
peek-char | read-char-no-hang | read-preserving-whitespace |
read | read-delimited-list | unread-char |
Figure 21–2. Operators relating to Input Streams.
The next figure shows operators relating to output streams.
clear-output | prin1 | write |
finish-output | prin1-to-string | write-byte |
force-output | princ | write-char |
format | princ-to-string | write-line |
fresh-line | print | write-string |
pprint | terpri | write-to-string |
Figure 21–3. Operators relating to Output Streams.
A stream that is both an input stream and an output stream is called a bidirectional stream. See the functions input-stream-p and output-stream-p.
Any of the operators listed in Figure 21–2 or Figure 21–3 can be used with bidirectional streams. In addition, the next figure shows a list of operators that relate specificaly to bidirectional streams.
y-or-n-p | yes-or-no-p |
Figure 21–4. Operators relating to Bidirectional Streams.
Streams are either open or closed.
Except as explicitly specified otherwise, operations that create and return streams return open streams.
The action of closing a stream marks the end of its use as a source or sink of data, permitting the implementation to reclaim its internal data structures, and to free any external resources which might have been locked by the stream when it was opened.
Except as explicitly specified otherwise, the consequences are undefined when a closed stream is used where a stream is called for.
Coercion of streams to pathnames is permissible for closed streams; in some situations, such as for a truename computation, the result might be different for an open stream and for that same stream once it has been closed.
An interactive stream is one on which it makes sense to perform interactive querying.
The precise meaning of an interactive stream is implementation-defined, and may depend on the underlying operating system. Some examples of the things that an implementation might choose to use as identifying characteristics of an interactive stream include:
read-char might wait for the user to type something before returning instead of immediately returning a character or end-of-file.
The general intent of having some streams be classified as interactive streams is to allow them to be distinguished from streams containing batch (or background or command-file) input. Output to batch streams is typically discarded or saved for later viewing, so interactive queries to such streams might not have the expected effect.
Terminal I/O might or might not be an interactive stream.
file-stream is used to represent a file stream.
The basic operation for opening a file is open, which typically returns a file stream (see its dictionary entry for details). The basic operation for closing a stream is close. The macro with-open-file is useful to express the common idiom of opening a file for the duration of a given body of code, and assuring that the resulting stream is closed upon exit from that body.
stream has a number of subclasses defined by this specification. The next figure shows some information about these subclasses.
Figure 21–5. Defined Names related to Specialized Streams
Certain stream variables are defined by this specification to be the proper source of input or output in various situations where no specific stream has been specified instead. A complete list of such standardized stream variables appears in the next figure. Added by agreement of Barrett, Loosemore, and KMP. -kmp 14-Feb-92The consequences are undefined if at any time the value of any of these variables is not an open stream.
Figure 21–6. Standardized Stream Variables
Note that, by convention, standardized stream variables have names ending in “-input*” if they must be input streams, ending in “-output*” if they must be output streams, or ending in “-io*” if they must be bidirectional streams.
User programs may assign or bind any standardized stream variable except *terminal-io*.
This list conjured by KMP, Barrett, and Loosemore. -kmp 14-Feb-92The operators in the next figure accept stream arguments that might be either open or closed streams.
Figure 21–7. Operators that accept either Open or Closed Streams
This list conjured by KMP, Barrett, and Loosemore. -kmp 14-Feb-92The operators in the next figure accept stream arguments that must be open streams.
Figure 21–8. Operators that accept Open Streams only
The consequences are undefined if the synonym stream symbol is not bound to an open stream from the time of the synonym stream's creation until the time it is closed.
stream System Classbroadcast-stream System Classconcatenated-stream System Classecho-stream System Classfile-stream System Classstring-stream System Classsynonym-stream System Classtwo-way-stream System Classinput-stream-p, output-stream-p Functioninteractive-stream-p Functionopen-stream-p Functionstream-element-type Functionstreamp Functionread-byte Functionwrite-byte Functionpeek-char Functionread-char Functionread-char-no-hang Functionterpri, fresh-line Functionunread-char Functionwrite-char Functionread-line Functionwrite-string, write-line Functionread-sequence Functionwrite-sequence Functionfile-length Functionfile-position Functionfile-string-length Functionopen Functionstream-external-format Functionwith-open-file Macroclose Functionwith-open-stream Macrolisten Functionclear-input Functionfinish-output, force-output, clear-output Functiony-or-n-p, yes-or-no-p Functionmake-synonym-stream Functionsynonym-stream-symbol Functionbroadcast-stream-streams Functionmake-broadcast-stream Functionmake-two-way-stream Functiontwo-way-stream-input-stream, two-way-stream-output-stream Functionecho-stream-input-stream, echo-stream-output-stream Functionmake-echo-stream Functionconcatenated-stream-streams Functionmake-concatenated-stream Functionget-output-stream-string Functionmake-string-input-stream Functionmake-string-output-stream Functionwith-input-from-string Macrowith-output-to-string Macro*debug-io*, *error-output*, *query-io*, *standard-input*, *standard-output*, *trace-output* Variable*terminal-io* Variablestream-error Condition Typestream-error-stream Functionend-of-file Condition Type