2
votes

I'm trying to write to a txt file using prolog so I tried making a simple predicate to test that, here it is:

writefacts():-
    open('output.txt', write, Out),
    write(Out, 'test'),
    close(Out).

The problem is that when I run it, on SWI-Prolog (AMD64, Multi-threaded, version 8.2.1), all I get is true and nothing on the output.txt.

I've already tried looking for similar questions, but all I learned is that this code should be working. Thanks for any help.

1
If you get true, then all the predicates succeeded. So the output.txt file should be created with the word test in it. Is the file not created? - rajashekar
It is working for me on swi-prolog same version and arch. - rajashekar

1 Answers

2
votes

I did not check your code but it does look correct.

Odds are it wrote to a directory different than what you expected.

To see the directory that SWI-Prolog is currently using as the working directory you can use either of these predicates.

?- pwd.
% c:/users/groot/documents/prolog/
true.

or

?- working_directory(D,D).
D = 'c:/users/groot/documents/prolog/'.

HTH


For more details see this post in the SWI-Prolog forum.