I'm trying to write to a file in Common Lisp, but I keep getting a "file is not a stream" error:
[1]> (open "file.txt" :direction :output :if-does-not-exist :create :if-exists :supersede)
#<output buffered file-stream character #P"file.txt">
[2]> (princ 'Hello "file.txt")
*** - princ: argument "file.txt" is not a stream
Even attempting to close the file returns an error:
[4]> (close "file.txt")
*** - no-applicable-method: When calling #<standard-generic-function close>
with arguments ("file.txt"), no method is applicable.
The file was properly created, so I thought it might be a permissions issue, but that doesn't seem to be it. I've Googled this error without any luck so far. Does anyone know what I am doing wrong? Thank you.
PS: I'm running Linux Mint 17.3 Rosa with CLISP 2.49 (2010-07-07)
To use open files, you must save the return value of open
and uses it as the second argument to princ
. You must also use that same return value as an argument to close
.
This is usually done with the convenience macro with-open-file.
The files chapter of Practical Common Lisp shows how to use these and other functions and macros.