首页 > 解决方案 > 如何在prolog中编写具有绝对路径的文件

问题描述

我当前的代码如下。如何为文件添加绝对路径或完整路径?我希望将文件保存在与当前工作目录不同的目录中。谢谢。

fileName('text.txt', Fname),
open(Fname, write, Stream1),
close(Stream1).

标签: prologswi-prolog

解决方案


只需使用带有/分隔符的路径(即使在 Windows 上)

选项有点过火:

?- open("/tmp/myfile.txt",write,Stream,alias(my_sink),encoding(utf8),type(text)]),
   write(Stream,foo(12,"Programmation en logique, très recommandée")),
   close(Stream).

然后有一个新文件:

$ file /tmp/myfile.txt 
/tmp/myfile.txt: UTF-8 Unicode text, with no line terminators

不确定如果您使用包含“保留字符”的路径会发生什么 - 其中一个是文件系统相关的。在 Windows 上有很多,在 Unix 文件系统上通常只有一个:/. open/N可能会抛出。

用于atomic_list_concat/3构造路径:

?- atomic_list_concat([my,parent,dir],'/',Path).
Path = 'my/parent/dir'.

推荐阅读