How to print '\n' instead of a newline?

前端 未结 11 533
南方客
南方客 2020-12-11 04:34

I am writing a program that uses prints a hex dump of its input. However, I\'m running into problems when newlines, tabs, etc are passed in and destroying my output formatti

相关标签:
11条回答
  • 2020-12-11 04:48

    The function printchar() below will print some characters as "special", and print the octal code for characters out of range (a la Emacs), but print normal characters otherwise. I also took the liberty of having '\n' print a real '\n' after it to make your output more readable. Also note that I use an int in the loop in main just to be able to iterate over the whole range of unsigned char. In your usage you would likely just have an unsigned char that you read from your dataset.

    #include <stdio.h>
    
    static void printchar(unsigned char theChar) {
    
        switch (theChar) {
    
            case '\n':
                printf("\\n\n");
                break;
            case '\r':
                printf("\\r");
                break;
            case '\t':
                printf("\\t");
                break;
            default:
                if ((theChar < 0x20) || (theChar > 0x7f)) {
                    printf("\\%03o", (unsigned char)theChar);
                } else {
                    printf("%c", theChar);
                }
            break;
       }
    }
    
    int main(int argc, char** argv) {
    
        int theChar;
    
        (void)argc;
        (void)argv;
    
        for (theChar = 0x00; theChar <= 0xff; theChar++) {
            printchar((unsigned char)theChar);
        }
        printf("\n");
    }
    
    0 讨论(0)
  • 2020-12-11 04:49

    As of C++11 you can also use raw strings

    std::printf(R"(\n)");
    

    everything inside the R"( and )" will be printed literally. escape sequences will not be processed.

    0 讨论(0)
  • 2020-12-11 04:50

    If you want to make sure that you don't print any non-printable characters, then you can use the functions in ctype.h like isprint:

    if( isprint( theChar ) )
      printf( "%c", theChar )
    else
      switch( theChar )
      {
      case '\n':
         printf( "\\n" );
         break;
      ... repeat for other interesting control characters ...
      default:
         printf( "\\0%hho", theChar ); // print octal representation of character.
         break;
      }
    
    0 讨论(0)
  • 2020-12-11 04:54

    You can escape the backslash to make it print just a normal backslash: "\\n".

    Edit: Yes you'll have to do some manual parsing. However the code to do so, would just be a search and replace.

    0 讨论(0)
  • 2020-12-11 04:58

    Just use String::replace to replace the offending characters before you call printf.

    You could wrap the printf to do something like this:

    void printfNeat(char* str)
    {
        string tidyString(str);
        tidyString.replace("\n", "\\n");
        printf(tidyString);
    }
    

    ...and just add extra replace statements to rid yourself of other unwanted characters.

    [Edit] or if you want to use arguments, try this:

    void printfNeat(char* str, ...)
    {
        va_list argList;
        va_start(argList, msg);
    
        string tidyString(str);
        tidyString.replace("\n", "\\n");
        vprintf(tidyString, argList);
    
        va_end(argList);
    }
    
    0 讨论(0)
  • 2020-12-11 04:59
    printf("\\n");
    
    0 讨论(0)
提交回复
热议问题