How to open a file in Lisp and specify both ':if-exists :append' and also ':if-does-not-exist :create' -
i trying make lisp code gracefully handles files both exist or don't exist.
so start file exists , has text "this original text not lisp text"
(defparameter s (open "s.txt" :direction :io :if-does-not-exist :create :if-exists :append)) =>s (print "first line" s) =>"first line" (print "second line" s) =>"second line" (read s) =>errors end of file reached. "; evaluation aborted on #<end-of-file {10045973c3}>." on cmd window: $cat s.txt original text not lisp text.
that full interaction. further more text in s.txt unchanged.
this fails append or overwrite text file @ all.
as rainer joswig pointed out, opening file not writing in piece of code. moreover, if want append, open file output direction. simple function open file , append this:
(defun write-smth-to-file (file-out smth) (with-open-file (f file-out :direction :output :if-exists :append :if-does-not-exist :create) "function open file , append it" (format f "~a~%" smth)))
Comments
Post a Comment