file-io - 重定向标准输出流

标签 file-io prolog prolog-toplevel

如何将 SWI-Prolog REPL 中 listing/0 的输出写入文件?

?- listing > file.txt.

最佳答案

您可以打开一个文件进行写入并将 current_ouput 重定向到它,如下所示:

?- current_output(Orig), % save current output
   open('file.txt', write, Out),
   set_output(Out),
   listing,
   close(Out),
   set_output(Orig). % restore current output

另外,SWI-Prolog 提供谓词 with_output_to/2可用于将当前输出重定向到一个目标。请务必阅读文档,但简而言之:

?- open('file.txt', write, Out),
   with_output_to(Out, listing),
   close(Out).

现在 listing/0 的输出将被写入 file.txt。 但请记住,里面会有很多东西。您可能想将 listing/1 用于特定谓词?在这种情况下,使用 clause/2portray_clause/2是另一种选择,特别是如果您想更好地控制写入文件的内容和方式。 listing 我猜只是为了交互使用。

关于file-io - 重定向标准输出流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32543457/

相关文章:

c - 写入此文件句柄有什么问题?

Prolog - 从事实列表中获取集合的最大值(使用失败谓词)

prolog - 如何在 Prolog 中同时返回变量结果和真/假?

android - 在Android中的Files中以二进制格式写入数据

c++ - ascii 和二进制 - 按应有的方式显示,但读取返回垃圾

list - 如何访问序言中的列表排列?

floating-point - PROLOG - 如何舍入 float 的小数?

比较两个文件的普通数和单个数

Prolog陷入无限循环