3

実行中の .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))))))

ファイルが見つからないのはなぜですか?

4

1 に答える 1

10

ファイルの場所を指定していないため、見つかりません。あなたが与えたのは名前/タイプだけで、ディレクトリはありませんでした。

この関数のファイルがどこにあるかは問題ではありません。パス名のコンテキストは設定しません。

通常、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))

ところで: 多くの場合、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.
于 2013-05-28T15:50:55.967 に答える