如何从文件中读取sexp

时间:2010-11-07 20:21:50

标签: lisp common-lisp

如果我使用

编写文件
(with-open-file (s "~/example.sexp" :direction :output)
           (write '(1 2 3) :stream s)
           (write '(4 5 6) :stream s)
           (write '(7 8 9) :stream s))

创建包含

的文件
(1 2 3)(4 5 6)(7 8 9)

但是当我尝试使用

打开并阅读它时
(setf f (open "~/example.sexp"))
(read :input-stream f)

我得到一个“:INPUT-STREAM不是STREAM类型”错误。

(type-of f)

返回STREAM :: LATIN-1-FILE-STREAM,看起来它至少是 close 到我需要的东西。有什么区别?

如何阅读我写入文件的列表?

2 个答案:

答案 0 :(得分:5)

你得到了READ错误的论据。它应该只是(read f),而不是(read :input-stream f)

答案 1 :(得分:4)

您也可以使用with-open-file:

(with-open-file (s "~/example.sexp")
  (read s))

甚至:

(with-open-file (*standard-input* "~/example.sexp")
  (read))

:输入是默认方向。

相关问题