Thread: Printing different representations of characters...

  1. #1
    Registered User
    Join Date
    Mar 2006

    Question Printing different representations of characters...

    First let me start by saying I am new to C. My program needs to read in a character in binary(including non-printable characters) and then display the character in octal representation, like the 'od' program does in UNIX. My code is as follows:

      int c;
      FILE *Fp;
      Fp = fopen(argv[1], "rb");
      while((c = fgetc(Fp)) != EOF)
          fprintf(stdout, "%06ho\t", c);
    The two program results are as follows:
    [solaris]$ myod test
    000150 000145 000154 000154 000157 000040 000167 000157 000162 000154 000144 000012

    [solaris]$ od test
    0000000 064145 066154 067440 073557 071154 062012
    "%06ho" was given to me for formatting purposes, so I know that is correct.

    I'll admit, I don't really understand...basically typing "myod test" should perform the same function as typing "od test", where test is a binary file.

    Thanks for your help.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Your int is probably signed. ho is for unsigned short. See this.

    Hope is the first step on the road to disappointment.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Consider using fread, perhaps like this.
       if ( Fp != NULL )
          unsigned short c;
          while ( fread(&c, sizeof c, 1, Fp) == 1 )
             printf("%06ho ", c);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Been here, done that.
    Join Date
    May 2003
    I remember answering this yesterday. Must have been on another forum...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-06-2008, 02:43 PM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. Printing only certain characters
    By dmkanz07 in forum C Programming
    Replies: 9
    Last Post: 04-18-2007, 07:40 AM
  4. printing non-ASCII characters (in unicode)
    By dbaryl in forum C Programming
    Replies: 1
    Last Post: 10-25-2002, 01:00 PM
  5. Printing extended ASCII characters
    By Jonny M in forum C Programming
    Replies: 2
    Last Post: 03-01-2002, 10:12 AM