Skip to main content

write-byte

write-byte Function

Syntax:

write-byte byte stream → byte

Arguments and Values:

byte—an integer of the stream element type of stream.

stream—a binary output stream.

Description:

write-byte writes one byte, byte, to stream.

Examples:

(with-open-file (s "temp-bytes" 
:direction :output
:element-type ’unsigned-byte)
(write-byte 101 s))101

Side Effects:

stream is modified.

Affected By:

The element type of the 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 output stream.

Might signal an error of type type-error if byte is not an integer of the stream element type of stream.

See Also:

read-byte, write-char, write-sequence

peek-char

Expanded Reference: write-byte

Basic Usage

write-byte writes one byte (an integer) to a binary output stream and returns the byte written.

(with-open-file (s "/tmp/cl-wb-test.bin"
:direction :output
:element-type '(unsigned-byte 8)
:if-exists :supersede)
(write-byte 101 s))
=> 101

Writing Multiple Bytes

(with-open-file (s "/tmp/cl-wb-test.bin"
:direction :output
:element-type '(unsigned-byte 8)
:if-exists :supersede)
(dolist (b '(72 101 108 108 111))
(write-byte b s)))
=> NIL

Round-Trip with read-byte

Data written with write-byte can be read back with read-byte.

(with-open-file (out "/tmp/cl-wb-rt.bin"
:direction :output
:element-type '(unsigned-byte 8)
:if-exists :supersede)
(write-byte 255 out)
(write-byte 0 out)
(write-byte 128 out))

(with-open-file (in "/tmp/cl-wb-rt.bin"
:element-type '(unsigned-byte 8))
(list (read-byte in) (read-byte in) (read-byte in)))
=> (255 0 128)