1

是否有一种或多或少的标准方法来输出包含特殊字符(如 ASCII 控制代码)的字符串,使得输出是有效的 C/C++ 文字字符串,带有转义序列?

预期输出示例:This\nis\na\\test\n\nShe said, \"How are you?\"\n

如果不小心,输出将是

This
is
a\test

She said, "How are you?"

不是想要什么。

4

3 回答 3

1

清楚地打印转义字符串很棘手。

问题包括

  1. 控制字符和空字符
  2. 转义转义字符。
  3. 负面char的。
  4. 十六进制转义序列没有指定的长度限制。八进制数限制为 3 位。

    void EscapePrint(char ch) {
      // 1st handle all simple-escape-sequence C11 6.4.4.4
      // The important one to detect is the escape character `\`
      // Delete or adjust these 2 arrays per code's goals
      static const char *escapev = "\a\b\t\n\v\f\r\"\'\?\\";
      static const char *escapec = "abtnvfr\"\'\?\\";
      char *p = strchr(escapev, (unsigned char) ch);
      if (p && *p) {
        printf("\\%c", escapec[p - escapev]);
      }
    
      // if printable, just print it.
      else if (isprint((unsigned char) ch)) {
        fputc(ch, stdout);
      }
    
      // else use octal escape  
      else {
        // Use octal as hex is problematic reading back
        printf("\\%03hho", ch);
      }
    }
    

Pedantic:八进制转义序列在char范围超过 9 位的稀有机器上是一个问题。这可以在字符串级别使用十六进制转义序列进行处理,而不是char逐个处理char,因为十六进制转义序列需要限制。例子:

  input 2 `char`:            \1 A
  //                               v----- intervening space
  output text including ":   "\\x1" "A"
于 2017-10-13T11:37:29.493 回答
0

根据您可能要实现的目标,我可以建议以下解决方案:只需将任何不可打印的字符替换为“\xnn”(nn 是字符的 ascii 代码。这归结为

void myprint (char *s) {
   while (*s) {
      printf(isalnum((unsigned char) *s) ? "%c" : "\\%03hho", *s);
      s++;
   }
}

这当然不会使用\n(but \x0a) 之类的特殊缩写,但这应该不是问题。

于 2017-10-13T10:37:42.963 回答
0

如果它对某人有帮助,我很快就把它放在一起:

void printfe(char* string)
{
    for (char c= *string; c; c= *(++string))
    {
        switch (c)
        {
        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: (c < ' ' || c > '~') ? printf("\\%03o", c) : putchar(c); break;
        }
    }
}

(注意潜在的不可移植性c < ' ' || c > '~',我想避免任何库引用)。

于 2017-10-13T10:54:14.003 回答