views:

187

answers:

1

Hi,

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 correct.

How to make it print proper path?

Any help please.

Thanks.

+1  A: 

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.

Kaarel
Thanks. Is there any way to add backslah in front of every backslash using any predicate in Prolog?
JPro
As far as I know there is no built-in predicate that would allow you to modify Prolog atoms like this, i.e. you need to implement this yourself: split the atom into a list of characters, walk through this list building a new list that has an additional backslash for every backslash, convert this list back into an atom.
Kaarel