Swi Prolog 读写文件

Swi Prolog Read Write with Files

所以我一直在尝试制作这个加密谓词,并且我让它适用于来自键盘和标准输出的输入,但我无法让它适用于文件,所以我需要帮助修复它。 我需要对 MessageFile 中的文本应用加密并将其写入 OutFile。

encrypt(MessageFile,Key,OutFile):- read(X),q(X,Key,R),!,string_to_list(S,R),
write(S).

正如我所说,它适用于标准 Input\Output:

9 ?- encrypt(X,a,Y). 
|   abcdef.             
 bcdefg          
 true.

如果相关,我的 .pl 文件以及 in.txt 和 out.txt 在 D:\Folder

您可以使用以下方式打开文件:

open(MessageFile, read, Read),

然后您可以使用 read/2 从文件流中读取 Prolog 术语:

read(Read, Term),

PS:请注意 string_to_list/2 已弃用,string_codes/2 是最新的等价物。