# Special character problem atom\_codes

**URL:** <https://swi-prolog.discourse.group/t/special-character-problem-atom-codes/3299>\
**Category:** Predicate\
**Created:** [November 24, 2020, 8:43am UTC](https://swi-prolog.discourse.group/t/special-character-problem-atom-codes/3299 "2020-11-24T08:43:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PrologAxel](https://avatars.discourse-cdn.com/v4/letter/p/ad7895/32.png) [@PrologAxel](https://swi-prolog.discourse.group/u/PrologAxel)\
**Post date:** [November 24, 2020, 8:43am UTC](https://swi-prolog.discourse.group/t/special-character-problem-atom-codes/3299/1 "2020-11-24T08:43:22Z")

</div>

Hello.

> **[SWI-Prolog -- atom\_codes/2](https://www.swi-prolog.org/pldoc/man?predicate=atom_codes%2F2)**

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

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [November 24, 2020, 9:19am UTC](https://swi-prolog.discourse.group/t/special-character-problem-atom-codes/3299/2 "2020-11-24T09:19:40Z")

</div>

> [@PrologAxel](#):
>
> 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),!”).

What you posted displays on this page as

```prolog
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

```prolog
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](https://www.swi-prolog.org/pldoc/man?section=charescapes)

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [November 24, 2020, 9:24am UTC](https://swi-prolog.discourse.group/t/special-character-problem-atom-codes/3299/3 "2020-11-24T09:24:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![PrologAxel](https://avatars.discourse-cdn.com/v4/letter/p/ad7895/32.png) [@PrologAxel](https://swi-prolog.discourse.group/u/PrologAxel)\
**Post date:** [November 24, 2020, 9:53am UTC](https://swi-prolog.discourse.group/t/special-character-problem-atom-codes/3299/4 "2020-11-24T09:53:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [November 24, 2020, 10:09am UTC](https://swi-prolog.discourse.group/t/special-character-problem-atom-codes/3299/5 "2020-11-24T10:09:38Z")

</div>

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.
