read-byte
read-byte Function
Syntax:
read-byte stream &optional eof-error-p eof-value → byte
Arguments and Values:
stream—a binary input stream.
eof-error-p—a generalized boolean. The default is true.
eof-value—an object. The default is nil.
byte—an integer , or the eof-value.
Description:
read-byte reads and returns one byte from stream.
If an end of file2 occurs and eof-error-p is false, the eof-value is returned.
Examples:
(with-open-file (s "temp-bytes"
:direction :output
:element-type ’unsigned-byte)
(write-byte 101 s)) → 101
(with-open-file (s "temp-bytes" :element-type ’unsigned-byte)
(format t "~S ~S" (read-byte s) (read-byte s nil ’eof)))
▷ 101 EOF
→ NIL
Side Effects:
Modifies stream.
Exceptional Situations:
Should signal an error of type type-error if stream is not a stream.
Should signal an error of type error if stream is not a binary input stream.
If there are no bytes remaining in the stream and eof-error-p is true, an error of type end-of-file is signaled.
See Also:
read-char, read-sequence, write-byte
Expanded Reference: read-byte
Basic Usage
read-byte reads one byte (an integer) from a binary input stream.
(with-open-file (s "/tmp/cl-bytes.bin"
:direction :output
:element-type '(unsigned-byte 8)
:if-exists :supersede)
(write-byte 65 s)
(write-byte 66 s)
(write-byte 67 s))
=> 67
(with-open-file (s "/tmp/cl-bytes.bin"
:element-type '(unsigned-byte 8))
(list (read-byte s)
(read-byte s)
(read-byte s)))
=> (65 66 67)
Handling End of File
When eof-error-p is nil, returns the eof-value at end of file instead of signaling an error.
(with-open-file (s "/tmp/cl-bytes.bin"
:element-type '(unsigned-byte 8))
(list (read-byte s nil :eof)
(read-byte s nil :eof)
(read-byte s nil :eof)
(read-byte s nil :eof)))
=> (65 66 67 :EOF)
Reading All Bytes
(with-open-file (s "/tmp/cl-bytes.bin"
:element-type '(unsigned-byte 8))
(loop for byte = (read-byte s nil nil)
while byte
collect byte))
=> (65 66 67)