Is there a standard C library function to escape C-strings?
For example, if I had the C string:
char example[] = \"first line\\nsecond line: \\\"inne
There is no standard C library function for this.
When you use the declaration
char example[] = "first line\nsecond line: \"inner quotes\"";
the escape sequences will be interpreted and replaced by the compiler. You will have to "un-interpret" the characters that C escapes. Here's a quick-n-dirty example:
#include
#include
void print_unescaped(char* ptr, int len) {
if (!ptr) return;
for (int i = 0; i < len; i++, ptr++) {
switch (*ptr) {
case '\0': printf("\\0"); break;
case '\a': printf("\\a"); break;
case '\b': printf("\\b"); break;
case '\f': printf("\\f"); break;
case '\n': printf("\\n"); break;
case '\r': printf("\\r"); break;
case '\t': printf("\\t"); break;
case '\v': printf("\\v"); break;
case '\\': printf("\\\\"); break;
case '\?': printf("\\\?"); break;
case '\'': printf("\\\'"); break;
case '\"': printf("\\\""); break;
default:
if (isprint(*ptr)) printf("%c", *ptr);
else printf("\\%03o", *ptr);
}
}
}