繁体   English   中英

带有特殊字符的字符串的控制台输出

[英]Console output of strings with special characters

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

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

如果不小心,输出将是

This
is
a\test

She said, "How are you?"

不是什么想要的。

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

问题包括

  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); } }

迂腐:八进制转义序列是char范围超过 9 位的罕见机器上的问题。 这可以用十六进制的字符串逃过级序列,而不是在处理charchar基础为十六进制转义序列需要的限制。 例子:

  input 2 `char`:            \1 A
  //                               v----- intervening space
  output text including ":   "\\x1" "A"

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

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

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

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

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 > '~'的潜在不可移植性,我想避免任何库引用)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM