LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Printing the octal representation of a character. (https://www.linuxquestions.org/questions/programming-9/printing-the-octal-representation-of-a-character-421089/)

smoothdogg00 03-02-2006 07:09 PM

Printing the octal representation of a character in C...
 
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. 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:
Quote:

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

jlliagre 03-02-2006 10:44 PM

Code:

od -b test
and
Code:

man od
will help you understanding what's going on.


All times are GMT -5. The time now is 10:41 PM.