如何在prolog中写一个绝对路径的文件?

问题描述 投票:0回答:1

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

fileName('text.txt', Fname),
open(Fname, write, Stream1),
close(Stream1).
prolog swi-prolog
1个回答
0
投票

只需使用路径与 / 分隔符

在选项上有点过头了。

?- 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'.
© www.soinside.com 2019 - 2024. All rights reserved.