0
votes

I am studying Prolog using SWI-Prolog.

I am finding some problems with the following example that simply access to a file and write in it what the user put in the Prolog Shell.

processFile(File) :- see(File),
                 processFile,
                 seen.

processFile :- read(Query),
               process(Query).

process(end_of_file) :- !.

process(Query) :- Query,
                  write(Query),
              nl,
              processFile.

The problem is that when, in the Prolog shell, I try to execute the processFile statement with a file name I obtain an error:

If I give a file name with an extension I obtain this error message (seems to interpret the extension as an operator):

7 ?- processFile(myFile.txt).
ERROR: Syntax error: Operator expected
ERROR: processFile(myFile
ERROR: ** here **
ERROR: .txt) . 

If I give a file name without extension I obtain this other error message (the file don't exist):

7 ?- processFile(myFile).
ERROR: see/1: source_sink `myFile' does not exist (No such file or directory)

So I have also try to create a new file named myFile (without extension) in the same folder where is the prolog source code file but I still again obtain:

8 ?- processFile(myFile).
ERROR: see/1: source_sink `myFile' does not exist (No such file or directory)

Why? where is the error? How can I solve it?

2
@closing_voters: the reason to close does not apply. It is not "difficult to tell what is being asked here". This is a very specific question about a specific problem with OP's code.Will Ness

2 Answers

9
votes

As @larsmans said, use single quotes.

"does not exist" error if you are sure that the file exists is probably because of that the current working directory is not what you are thinking it is.

Try to change the current working directory to where your .txt file exists with cd('directory-path-here').

1
votes

You need to quote the filename.

processFile('myFile.txt').