0
votes

I am studying Prolog using SWI Prolog and I am finding some difficulties related to the operation of reading and writing on a file.

I have this simple program that read something from the standard input (the keyboard) and write it on a file:

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

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

process(end_of_file) :- !.

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

I am trying to execute it under Linux. So in the bash I go into the folder where it is located the Prolog source file and the myFile file and, after that I have consult the program, I execute the following statment:

?- processFile(myFile).
true.

as you can see the probem is that give me true but I can't insert anything by the keyboard, so anything could be write into myFile file.

If I try to trace what happen I obtain the following informations:

[trace]  ?- processFile(myFile).
   Call: (6) processFile(myFile) ? creep
   Call: (7) see(myFile) ? creep
   Exit: (7) see(myFile) ? creep
   Call: (7) processFile ? creep
   Call: (8) read(_G697) ? creep
   Exit: (8) read(end_of_file) ? creep
   Call: (8) process(end_of_file) ? creep
   Exit: (8) process(end_of_file) ? creep
   Exit: (7) processFile ? creep
   Call: (7) seen ? creep
   Exit: (7) seen ? creep
   Exit: (6) processFile(myFile) ? creep
true.

As you can see it can access to myFile file, and correctly ask the processFile predicate that contain the read statment but seems that automatically read the end_of_file atom and can't go on.

Why? what can I do to solve it and write into this file?

1
please, post (part of) the file...CapelliC
what file? myFile? it is empty (I have an empty file in which I would write into what I read from the keyboard) What I am missing? tnx so muchAndreaNobili

1 Answers

0
votes

You must use tell/1 to write in a file, then told to revert to previous. Using see/1 you're reading from that, and it's empty...

But ISO IO is better...