By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,988 Members | 1,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,988 IT Pros & Developers. It's quick & easy.

binary file reading in C

P: 2
Hello all,

I'm trying to read in a binary file in C. I have the specification of the file format and everything. I can parse the file with a software called "010 Editor". This way I can check if the values I read in in my C program are correct.

The problem I have is parsing 32 bit integer values in C.

This is the pretty simple code:
Expand|Select|Wrap|Line Numbers
  1.     int temp = 0;
  2.     fread(&temp,sizeof(int),1,fp);
  3.     printf("%i\t",temp);
It works fine for most of the values in the file. But for some values in the file it is just not printing out the correct integer value.

E.g. :
binary file hex values: 8A 1A 32 01
integer value I get in C: 138
It should be : 20060810

-> 138 is the unsigned byte value of 8A. Am I just reading in 8A?

For the following example it works:
binary file hex values: E1 7A 7F 00
integer value I get in C: 8354529
It should be : 8354529

I also tried to read in the data bytewise from the file and combinig it to an integer by shifting bytewise.

Expand|Select|Wrap|Line Numbers
  1.     int temp = 0;
  2.     temp  = getc(fp);
  3.     temp |= getc(fp) << 8;
  4.     temp |= getc(fp) << 16;
  5.     temp |= getc(fp) << 24;
  6.     printf("%i\t",temp);
The results are the same...

I'm using the MS Visual Studio compiler on Windows.
I'm stuck with this problem for quite a while now and I would appreciate any help I can get.

Thank You!
Sep 15 '06 #1
Share this Question
Share on Google+
4 Replies

Expert Mod 5K+
P: 8,916
Have you opened the file in binary mode (add a b to the end of the mode string) because

0x1A = 26 = ASCII Ctrl-Z

which is often(well used to be) used to indicate the end of a text file so the read could be stopping because it thinks it has reached the end of file if you have opened the file in text mode.
Sep 15 '06 #2

P: 2
Thank you very much!

I think that solved my problem. I never thought about this..
I just added that "b" and everything seems to work now.

I also have to read in 24 bit and 8 bit integers.
I thought this piece of code should do the trick:

Expand|Select|Wrap|Line Numbers
  1. int read_int24(FILE *fp)
  2. {
  3.     int temp = 0;
  4.     temp  = (int)getc(fp);
  5.     temp |= (int)getc(fp) << 8;
  6.     temp |= (int)getc(fp) << 16;
  8.     return temp;
  9. }
  11. int read_int8(FILE *fp)
  12. {
  13.     int temp = (int)getc(fp);
  14.     return temp;
  15. }
It turns out that I'm just reading in 255 or 0 for 8 bit integers. I'm also not sure about the 24 bit integer values.
Do you have an idea what I'm doing wrong here? I think it's somthing with those byte-shifting operators I don't understand... :(
Maybe there is also an easier way to do this?

Thanks again, you are a great help!
Sep 18 '06 #3

Expert Mod 5K+
P: 8,916
That code looks correct to me. The only possibility I can think of is that you have got to the end of the file. At the end of the file getc returns EOF and that often has a value -1 (all bits set), but that doesn't explain the 0's.

I would be stepping through the code with a symbolic debugger at this point.
Sep 18 '06 #4

P: 1
I have a problem in reading binary files plz help
i have used fread function to read in binary files
Expand|Select|Wrap|Line Numbers
  1. Header *head;//structure holding 
  2. if ((fp = fopen("abc.bin", "rb+")) == NULL) return -1;
  3.     bytes_read=fread((char *)head, sizeof(Header ),1, fp);
  4.     //bytes_read = fread(buf, sizeof(buf), 1, fp);
  5.     if ( bytes_read ) {
  6.                 printf( "Number of characters has been read = %i\n", bytes_read );
  7.         printf( "buffer = %s\n", head);}
But this code reads only 1st byte
I want to read the entire file .. How can i do tht.. plz help...Also how can i get the values from binary file in the structure variable
Jul 18 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.