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

前端 未结 11 575
南方客
南方客 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: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;
      }
    

提交回复
热议问题