Printing different representations of characters...

This is a discussion on Printing different representations of characters... within the C Programming forums, part of the General Programming Boards category; First let me start by saying I am new to C. My program needs to read in a character in ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    Maine
    Posts
    11

    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:

    Code:
      int c;
    
      FILE *Fp;
      Fp = fopen(argv[1], "rb");
    
      while((c = fgetc(Fp)) != EOF)
       {
          fprintf(stdout, "%06ho\t", c);
        }
    
      fclose(Fp);
    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
    0000014
    "%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
    Posts
    14,826
    Your int is probably signed. ho is for unsigned short. See this.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Consider using fread, perhaps like this.
    Code:
       if ( Fp != NULL )
       {
          unsigned short c;
          while ( fread(&c, sizeof c, 1, Fp) == 1 )
          {
             printf("%06ho ", c);
          }
          fclose(Fp);
       }
    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
    Posts
    1,156
    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, 01: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, 09:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21