LISP无法找到要打开的文件

lin*_*ndy 3 lisp common-lisp

我正在尝试打开一个文件,该文件与我正在运行的.lsp文件位于同一文件夹中,但它给了我这个错误: Error: No such file or directory : "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))))))
Run Code Online (Sandbox Code Playgroud)

为什么找不到文件?

Rai*_*wig 9

它找不到,因为你没有说文件的位置.你给的只是一个名字/类型而没有目录.

这个函数的文件无关紧要.它不设置路径名的上下文.

通常,像Clozure CL这样的东西会在默认情况下查看它所在的目录.

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

您对CCL的选择:

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

您还可以根据要加载的源文件计算路径名.你需要在文件中这样的东西:

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

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

顺便说一句:通常一个Lisp监听器不仅包含'REPL'(读取Eval打印循环),还支持'命令'.CCL就是这样一个案例.查看CCL提供的命令:help.在调试器中还有不同的/更多命令.

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

来自Clozure的例子在Mac上的IDE中运行的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.
Run Code Online (Sandbox Code Playgroud)