MuchMemezMuchWow
MuchMemezMuchWow

Reputation: 3

C program to store special characters in strings

Basically, I can't figure this out, I want my C program to store the entire plaintext of a batch program then insert in a file and then run.

I finished my program, but holding the contents is my problem. How do I insert the code in a string and make it ignore ALL special characters like %s \ etc?

Upvotes: 0

Views: 6664

Answers (2)

user3553031
user3553031

Reputation: 6214

As Ian previously mentioned, you can escape characters that aren't allowed in normal C strings with \; for instance, newline becomes \n, double-quote becomes \", and backslash becomes \\.

If you're unable or unwilling to do this for whatever reason, then you may be out of luck if you're solution must be in C. However, if you're willing to switch to C++, then you can use raw strings:

const char* s1 = R"foo(
Hello
World
)foo";

This is equivalent to

const char* s2 = "\nHello\nWorld\n";

A raw string must begin with R" followed by an arbitrary delimiter (made of any source character but parentheses, backslash and spaces; can be empty; and at most 16 characters long), then (, and must end with ) followed by the delimiter and ". The delimiter must be chosen such that the termination substring (), delimiter, ") does not appear within the string.

Upvotes: 1

Bob
Bob

Reputation: 1851

You have to escape special characters with a \, you can escape backslash itself with another backslash (i.e. \\).

Upvotes: 1

Related Questions