how to display blackshashes in SWI-Prolog
I am trying to display a network share path in my Prolog output code.
The path is like :
\\fileserver\path\to\file.txt (ex1)
or
\\\\fileserver\\path\\to\\file.txt (ex2)
but If I try displaying it using format :
pri(Z):-
format('Printing Zx : \"~w\"',[Z]).
the slashes get truncated to
\fileserverpathtofile.txt (ex1)
Obviously some times, the path may contain \\\\
in which case the display is开发者_Go百科 correct.
How to make it print proper path?
Any help please.
Thanks.
In the Prolog atoms backslash is a meta-character, i.e. if you want your atom to contain a backslash character then you need to escape it using the backslash character. E.g. in order to represent the Windows path \\fileserver\path\to\file.txt
as a Prolog atom you need to write
Path = '\\\\fileserver\\path\\to\\file.txt'.
In principle there are two ways of printing stuff out, one for the humans (pretty-printing), using write
?- Path = '\\\\fileserver\\path\\to\\file.txt', write(Path).
\\fileserver\path\to\file.txt
and one for the machines (serializing), using write_canonical
?- Path = '\\\\fileserver\\path\\to\\file.txt', write_canonical(Path).
'\\\\fileserver\\path\\to\\file.txt'
write_canonical
makes sure that Prolog can read the output back into the same exact atom.
Your problem seems to be that you do not correctly represent the path in Prolog. If the path comes from an external source, you first need to escape it (add a backslash in front of every backslash) before you can store it as a Prolog atom.
精彩评论