views:

938

answers:

9

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 formatting.

How can I use printf (or cout I guess) to print '\n' instead of printing an actual newline? Do I just need to do some manual parsing for this?

EDIT: I'm receiving my data dynamically, it's not just the \n that I'm corned about, but rather all symbols. For example, this is my printf statement:

printf("%c", theChar);

How can I make this print \n when a newline is passed in as theChar but still make it print normal text when theChar is a valid printable character?

+4  A: 

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.

Jon-Eric
+19  A: 

Print "\\n" – "\\" produces "\" and then "n" is recognized as an ordinary symbol.

sharptooth
+3  A: 

Just use "\\n" (two slashes)

Samuel Carrijo
+4  A: 
printf("\\n");
Nik
+1  A: 

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);
}
Jon Cage
+13  A: 

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");
}
Jared Oberhaus
+1: Good idea on the < 20 and > 127 catch
Jon Cage
mark4o
Thanks @mark4o, you're right about the 0x20 and the 32. Fixed my answer.
Jared Oberhaus
+2  A: 

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;
  }
Peter Kovacs
+2  A: 

In addition to the examples provided by other people, you should look at the character classification functions like isprint() and iscntrl(). Those can be used to detect which characters are or aren't printable without having to hardcode hex values from an ascii table.

TheUndeadFish
+1  A: 

In C/C++, the '\' character is reserved as the escape character. So whenever you want to actually print a '\', you must enter '\'. So to print the actual '\n' value you would print the following:

printf("\\n");
Mr. Will