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,
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).