简体   繁体   English

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

[英]Console output of strings with special characters

Is there a more or less standard way to output character strings that contain special characters, such ASCII control codes, in such a way that the output is a valid C/C++ literal string, with escape sequences ?是否有一种或多或少的标准方法来输出包含特殊字符的字符串,例如 ASCII 控制代码,这样输出是有效的 C/C++ 文字字符串,带有转义序列?

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

Without care, the output would be如果不小心,输出将是

This
is
a\test

She said, "How are you?"

not what want.不是什么想要的。

Printing escaped strings clearly is tricky.清楚地打印转义字符串很棘手。

Problems include问题包括

  1. Control characters and the null character .控制字符和空字符
  2. Escaping the escape character.转义转义字符。
  3. Negative char .char
  4. Hexadecimal escape sequence have no specified limit in length.十六进制转义序列的长度没有规定的限制。 Octal ones are limited to 3 digits.八进制数限制为 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: the octal escape sequence is a problem on rare machines whose char range exceeds 9 bits.迂腐:八进制转义序列是char范围超过 9 位的罕见机器上的问题。 This can be handled with a hex escaped sequences at the string level and not on a char by char basis as hex escaped sequences need a limit.这可以用十六进制的字符串逃过级序列,而不是在处理charchar基础为十六进制转义序列需要的限制。 Example:例子:

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

Depending on what you might be trying to achieve I can suggest the following solution: Just replace any non-printable character with "\\xnn" (nn being the ascii code of the character. This would boil down to根据您可能想要实现的目标,我可以建议以下解决方案:只需将任何不可打印的字符替换为“\\xnn”(nn 是字符的 ascii 代码。这将归结为

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

This will of course not use special abbreviations like \\n (but \\x0a ) instead, but this shouldn't be a problem.这当然不会使用像\\n (而是\\x0a )这样的特殊缩写,但这应该不是问题。

If it helps someone, I have quickly put this together:如果它对某人有帮助,我很快就把它放在一起:

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

(mind the potential non-portability of c < ' ' || c > '~' , I wanted to avoid any library reference). (请注意c < ' ' || c > '~'的潜在不可移植性,我想避免任何库引用)。

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

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