Reputation: 17900
I am trying to open a file which resides in the same folder as the .lsp file I am running, but it gives me this error: Error: No such file or directory : "a.txt"
Here is the code I use:
(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))))))
Why isn't the file found?
Upvotes: 3
Views: 2176
Reputation: 139241
It's not found because you have not said where the file is. All you gave was a name/type and no directory.
Where the file for this function is does not matter. It does not set the context for pathnames.
Typically something like Clozure CL will look in the directory where it has been started from, by default.
Additionally Common Lisp has a variable *default-pathname-defaults*
. You can set or bind the defaults for pathnames there.
Your options for CCL:
(:cd "/mydir/foo/bar/")
. This is specific to CCL*default-pathname-defaults*
You can also compute the pathname based on the source file you are loading. You would need something like this in the file:
(defvar *my-path* *load-pathname*)
(let ((*default-pathname-defaults* (or *my-path*
(error "I have no idea where I am"))))
(readfile))
Btw.: often a Lisp listener does not only consist of a 'REPL' (Read Eval Print Loop), but also supports 'commands'. CCL is such a case. To see what commands CCL provides use :help
. In the debugger there are also different/more commands.
It is very useful that Clozure CL provides commands find or set the current directory. Other CL implementations provide similar functionality - but in a different way, since there is no standard for the command mechanism (besides CLIM) and the default commands.
Example from Clozure Common Lisp running in the IDE on a Mac:
? :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.
Upvotes: 10