我正在使用SWI-Prolog学习Prolog.
我发现以下示例存在一些问题,只需访问文件并在其中写入用户放在Prolog Shell中的内容.
processFile(File) :- see(File),
processFile,
seen.
processFile :- read(Query),
process(Query).
process(end_of_file) :- !.
process(Query) :- Query,
write(Query),
nl,
processFile.
Run Code Online (Sandbox Code Playgroud)
问题是,在Prolog shell中,当我尝试使用文件名执行processFile语句时,我获得了一个错误:
如果我给一个带扩展名的文件名,我会收到此错误消息(似乎将扩展名解释为运算符):
7 ?- processFile(myFile.txt).
ERROR: Syntax error: Operator expected
ERROR: processFile(myFile
ERROR: ** here **
ERROR: .txt) .
Run Code Online (Sandbox Code Playgroud)
如果我给出一个没有扩展名的文件名,我会得到另一条错误消息(该文件不存在):
7 ?- processFile(myFile).
ERROR: see/1: source_sink `myFile' does not exist (No such file or directory)
Run Code Online (Sandbox Code Playgroud)
所以我也尝试在prolog源代码文件所在的同一个文件夹中创建一个名为myFile(没有扩展名)的新文件,但我仍然再次获得:
8 ?- processFile(myFile).
ERROR: see/1: source_sink `myFile' does not exist (No such file or directory)
Run Code Online (Sandbox Code Playgroud)
为什么?错误在哪里?我该如何解决?
正如@larsmans所说,使用单引号.
"不存在"错误,如果您确定该文件存在可能是因为当前工作目录不是您认为的那样.
尝试将当前工作目录更改为.txt文件所在的位置cd('directory-path-here').