2

I have a problem in Prolog regarding adding new facts to the file 'relations.pl'. Every time I get facts I save them and I use

tell('relations.pl').
listing(relation).
told.

The only problem is that I want to insert the new facts and avoid storing the same facts more that one if there are any.

Is there anyway to do this? Thank you,

Alaa
  • 71
  • 5
  • You could append text to a file, but be warned, this easily leads to a lot of errors and inconsistencies. Instead write it all at once. – false Jun 29 '14 at 16:15

1 Answers1

1

It is a bit more robust to say out_tofile(listing(relation),'relation.pl'). The only inplace operation for textfiles is to append new text to them. I cannot recommend doing this here. Appending would be fine for logfiles.

:- meta_predicate
      out_tofile(0,+),             % out_tofile(:,+) in older versions
      out_ontofile(0,+),           % idem
      out_tostream__andclose(0,+). % idem

out_tofile(Goal, File) :-
   open(File,write,Stream),
   out_tostream__andclose(Goal, Stream).

out_ontofile(Goal, File) :-
   open(File,append,Stream),
   out_tostream__andclose(Goal, Stream).

out_tostream__andclose(Goal, Stream) :-
   current_output(Stream0),
   call_cleanup((set_output(Stream),once(Goal)), set_output_close(Stream0, Stream)).

set_output_close(Stream0, Stream) :-
   set_output(Stream0),
   close(Stream).
false
  • 10,264
  • 13
  • 101
  • 209