【发布时间】:2021-08-23 10:21:21
【问题描述】:
我正在尝试使用 Common Lisp with-open-file 读取文件。在我的环境中,我使用的是 SBCL、Emacs 和 Slime。
我可以使用以下命令打开像 this one (bookmarks-to-read.lisp) 这样的文件:
(defun read-my-file ()
(with-open-file
(stream "~/miscellaneous/misc/symbolic-computation/bookmarks-to-read.lisp")
(let ((contents
(read-all-objects stream (list '$eof$))))
(format t "~&Read ~S objects from the file."
(length contents))
contents)))
(defun read-all-objects (stream eof-indicator)
(let ((result (read stream nil eof-indicator)))
(if (eq result eof-indicator)
nil
(cons result (read-all-objects stream eof-indicator)))))
在 REPL 上调用该函数后,我得到了预期的内容:
CL-USER> (read-my-file)
Read 1 objects from the file.
(((:URL "https://calendar.google.com/calendar/u/0/r/week?pli=1" :TITLE
.
.
.
(:URL "https://www.rescuetime.com/dashboard" :TITLE
"RescueTime - Your Daily dashboard" :DATE
"2021-05-16T23:08:46.605691Z" :TAGS ("rescue"))))
注意这是.lisp。但是,如果我用这个 other file (history-to-read.lisp) 尝试同样的事情,我会收到一个错误。
我只需要更改位置:
(defun read-my-file ()
(with-open-file
(stream "~/miscellaneous/misc/symbolic-computation/history-to-read.lisp")
(let ((contents
(read-all-objects stream (list '$eof$))))
(format t "~&Read ~S objects from the file."
(length contents))
contents)))
(defun read-all-objects (stream eof-indicator)
(let ((result (read stream nil eof-indicator)))
(if (eq result eof-indicator)
nil
(cons result (read-all-objects stream eof-indicator))))
然后,在 REPL 上调用函数后:
CL-USER> (read-my-file) ; Evaluation aborted on
#<SB-INT:SIMPLE-READER-PACKAGE-ERROR "Package ~A does not exist." {1003AB9273}>.
我收到此错误消息:
包 HISTORY-TREE 不存在。
行:3,列:45,文件位置:110
流:#
[类型条件 SB-INT:简单阅读器包错误]
“HISTORY-TREE”只是写在文本上的东西。喜欢 foo。
此外,我尝试重现@RainerJoswig here 描述的建议。不幸的是,同样的问题发生了。
1 - 由于两个文件都是.lisp 并且读取它们的功能相同,为什么会发生这种情况?
2 - 为什么文本的含义/内容在这里很重要?
3 - 我怎样才能读取这个文件?
【问题讨论】:
标签: package common-lisp reader