Special character problem atom_codes

Hello.

atom_codes(A, “read_file(_2678,[_2666|_2668]):- \+at_end_of_stream(_2678),read_line_to_codes(_2678,_2790),atom_chars(_2666,_2790),read_file(_2678,_2668),!”).
Does not work because of the “\” character.

What can I do? The code between “” comes from another source. I do not control it.

Thank you

What you posted displays on this page as

atom_codes(A, “read_file(_2678,[_2666|_2668]):- \+at_end_of_stream(_2678),read_line_to_codes(_2678,_2790),atom_chars(_2666,_2790),read_file(_2678,_2668),!”).

but what you posted is

atom_codes(A, “read_file(_2678,[_2666|_2668]):- \\+at_end_of_stream(_2678),read_line_to_codes(_2678,_2790),atom_chars(_2666,_2790),read_file(_2678,_2668),!”).

In other words one of the \ is not displaying in your original post.

Does what you are having problems with have one or two \?


For others finding this post.

See: SWI-Prolog Character Escape Syntax

1 Like

It is not really clear how you got into this situation. Seems some external program generated the above program code? If that is the case, this external program is broken as a \ inside a Prolog string needs to be escaped. The _NNN variables suggest this external program is in Prolog too? In that case one must merely use quoted write to write a string instead of writing a quote, the content and the closing quote. See writeq/1, write_term/2,3 and the ~q sequence in format/2,3

1 Like

Hello.

Thank you for the answers.

I remembered how to solve it. I will search for the character ‘\’
and then replace it with two times ‘\’. Then the code works.

1 Like

Don’t forget that you also need to replace the quote with \" and if you want to stay within ISO replace newlines with \n. If you use otherwise normal printable ASCII characters you should be safe. The general case dealing with full Unicode is not so easy. If you can, better avoid creating strings that must be interpreted by some other language.

1 Like