保存到Lisp中的文件

时间:2011-03-26 06:32:28

标签: file-io lisp common-lisp

我只是想用以下函数写一个文件:

(defun test-save ()
  (with-open-file (stream "test.txt" :if-does-not-exist :create)
                  (format stream "success!")))

但是,输入(test-save)会产生以下内容: errors galore

我在这里做错了什么?

如果重要的话,我在Mac上使用带有SBCL的Cusp for Eclipse。

更新:现在出现这个新错误: new error

和repl:

COMMON-LISP-USER>
(with-open-file (stream "test.txt" :direction :output
                                   :if-does-not-exist :create)
      (format stream "success!"))

error opening #P"/Applications/eclipse/Eclipse.app/Contents/MacOS/test.txt":
  File exists
   [Condition of type SB-INT:SIMPLE-FILE-ERROR]
    0: [ABORT] Return to SLIME's top level.
    1: [TERMINATE-THREAD] Terminate this thread (#<THREAD "repl-thread" {12539CB1}>)
]> 0

更新2:

解决!我只需要使用:if-exists :supersede

1 个答案:

答案 0 :(得分:6)

尝试添加:direction :output以创建输出文件流:

(with-open-file (stream "test.txt" :direction :output
                                   :if-does-not-exist :create)
      (format stream "success!"))