LISP 找不到要打开的文件

标签 lisp common-lisp

我试图打开一个与我正在运行的 .lsp 文件位于同一文件夹中的文件,但它给我这个错误:错误:没有这样的文件或目录:“a.txt”

这是我使用的代码:

(defun readfile ()
 (let (lines columns matrix)
  (with-open-file (file "a.txt")
   (setq lines (parse-integer (read-char file)))
   (setq columns (parse-integer (read-char file))))))

为什么找不到文件?

最佳答案

没有找到,因为你没有说文件在哪里。您所提供的只是一个名称/类型,没有目录。

这个函数的文件在哪里并不重要。它不设置路径名的上下文。

通常情况下,像 Clozure CL 这样的东西会在默认情况下在其启动的目录中查找。

另外 Common Lisp 有一个变量 *default-pathname-defaults*。您可以在那里设置或绑定(bind)路径名的默认值。

您对 CCL 的选择:

  • 在正确的目录中启动 CCL
  • 使用 (:cd "/mydir/foo/bar/") 在 REPL 中设置当前目录。这是 CCL 特有的
  • 设置或绑定(bind)*default-pathname-defaults*

您还可以根据正在加载的源文件计算路径名。您需要在文件中包含以下内容:

(defvar *my-path* *load-pathname*)

(let ((*default-pathname-defaults* (or *my-path*
                                       (error "I have no idea where I am"))))
  (readfile))

顺便说一句:Lisp 监听器通常不仅包含“REPL”(Read Eval Print Loop),而且还支持“命令”。 CCL就是这样的情况。要查看 CCL 提供的命令,请使用 :help。在调试器中也有不同的/更多的命令。

Clozure CL 提供查找或设置当前目录的命令非常有用。其他 CL 实现提供类似的功能 - 但方式不同,因为没有针对命令机制(除了 CLIM)和默认命令的标准。

在 Mac 上的 IDE 中运行的 Clozure Common Lisp 示例:

? :help
The following toplevel commands are available:
 :KAP   Release (but don't reestablish) *LISTENER-AUTORELEASE-POOL*
 :SAP   Log information about current thread's autorelease-pool(s)
        to C's standard error stream
 :RAP   Release and reestablish *LISTENER-AUTORELEASE-POOL*
 :?     help
 :PWD   Print the pathame of the current directory
 (:CD DIR)  Change to directory DIR (e.g., #p"ccl:" or "/some/dir")
 (:PROC &OPTIONAL P)  Show information about specified process <p>
                      / all processes
 (:KILL P)  Kill process whose name or ID matches <p>
 (:Y &OPTIONAL P)  Yield control of terminal-input to process
whose name or ID matches <p>, or to any process if <p> is null
Any other form is evaluated and its results are printed out.

关于LISP 找不到要打开的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16795603/

相关文章:

lisp - 为什么 Common Lisp 打印输出前面有一个换行符,后面有一个空格?

list - 使用 Scheme 递归添加到列表

计算列表中出现 1 2 次的 Lisp 函数

assembly - 了解 SBCL 进入/退出程序集样板代码

conditional-statements - Lisp - 无论如何执行 "when"条件?

function - 如何在 Racket 中将 `and` 作为函数传递?

emacs - 加载远程 Lisp 文件

lisp - 关于 LISP 中关于格式函数的语句的解释

lisp - 奇怪的路径名?

functional-programming - 在 Common Lisp 中反转多个值的顺序