我想使用if exits,但不知道如何使用,add给出错误,-当我尝试覆盖它时,会更改文件
(defun writeToFile (filename content)
(with-open-file (stream filename :external-format charset:iso-8859-1
:direction :output
;if-exists :add
:if-does-not-exist :create )
(format stream content)
(terpri stream)))
(loop for i from x to y
do (if (= (is_me i) 0)
(format t "i = ~d ~%" i)
(writeToFile "/home/out.txt"
(concatenate 'string (write-to-string i) " is me" )))
do (if (ime i)
(format t "~d IS ME~%" i)
(writeToFile "/home/out.txt"
(concatenate 'string (write-to-string i) " is me" ))))
快速回答,您需要使用:if-exists :append
。
Common Lisp HyperSpec对开放有以下几点看法:
如果存在---其中之一:error、:new version、:rename、:rename-and-delete、:overwrite、:append、:replace或nil。如果filespec的版本组件为:最新,则默认值为:new version,否则为:error。
如果我们看看它对:append
:的描述
:附加
流上的输出操作会破坏性地修改现有文件。文件指针最初位于文件的末尾。如果方向为:io,则文件以双向模式打开,允许读取和写入。