How do I write the output of listing/0
in SWI-Prolog REPL to a file ?
?- listing > file.txt.
You can open a file for writing and redirect current_ouput
to it like this:
?- current_output(Orig), % save current output
open('file.txt', write, Out),
set_output(Out),
listing,
close(Out),
set_output(Orig). % restore current output
Alternatively, SWI-Prolog provides a predicate with_output_to/2
which can be used to redirect the current output for one goal. Make sure to read the documentation, but in short:
?- open('file.txt', write, Out),
with_output_to(Out, listing),
close(Out).
Now the output of listing/0
will be written to file.txt
.
But keep in mind that there is going to be a whole lot of stuff in there. You maybe want to use listing/1
for specific predicates? In this case, using clause/2
and portray_clause/2
is another option, especially if you want more control over what and how you are writing to the file. listing
is meant only for interactive use I guess.