Skip to main content

close

close Function

Syntax:

close stream &key abort → result

Arguments and Values:

stream—a stream (either open or closed).

abort—a generalized boolean. The default is false.

resultt if the stream was open at the time it was received as an argument, or implementation dependent otherwise.

Description:

close closes stream. Closing a stream means that it may no longer be used in input or output operations. The act of closing a file stream ends the association between the stream and its associated file; the transaction with the file system is terminated, and input/output may no longer be performed on the stream.

If abort is true, an attempt is made to clean up any side effects of having created stream. If stream performs output to a file that was created when the stream was created, the file is deleted and any previously existing file is not superseded.

It is permissible to close an already closed stream, but in that case the result is implementation dependent.

After stream is closed, it is still possible to perform the following query operations upon it: streamp, pathname, truename, merge-pathnames, pathname-host, pathname-device, pathname-directory,pathname-name, pathname-type, pathname-version, namestring, file-namestring, directory-namestring, host-namestring, enough-namestring, open, probe-file, and directory.

The effect of close on a constructed stream is to close the argument stream only. There is no effect on the constituents of composite streams.

For a stream created with make-string-output-stream, the result of get-output-stream-string is unspecified after close.

Examples:

(setq s (make-broadcast-stream)) → #<BROADCAST-STREAM> 
(close s) → T
(output-stream-p s) → true

Side Effects:

The stream is closed (if necessary). If abort is true and the stream is an output file stream, its associated file might be deleted.

See Also:

open

Expanded Reference: close

Basic Usage

close closes a stream so it can no longer be used for I/O. It returns T if the stream was open.

(let ((s (make-broadcast-stream)))
(close s))
=> T

Checking Open Status After Close

After closing, the stream still satisfies streamp but open-stream-p returns false.

(let ((s (make-string-output-stream)))
(close s)
(list (streamp s) (open-stream-p s)))
=> (T NIL)

Closing an Already Closed Stream

It is permissible to close an already closed stream.

(let ((s (make-string-output-stream)))
(close s)
(close s)) ; No error; result is implementation-dependent

The :abort Keyword

When :abort is true and the stream is an output file stream, the file may be deleted rather than kept.

;; Normal close keeps the file
(with-open-file (s "/tmp/cl-close-test.txt"
:direction :output :if-exists :supersede)
(write-string "data" s))
=> "data"

;; With :abort t, the implementation may delete the file
(let ((s (open "/tmp/cl-close-abort.txt"
:direction :output :if-exists :supersede)))
(write-string "tentative data" s)
(close s :abort t))

Query Operations on Closed Streams

Certain pathname-related queries remain valid on closed file streams.

(let ((s (open "/tmp/cl-close-test.txt" :direction :probe)))
(list (streamp s)
(pathnamep (pathname s))))
=> (T T)