1. are you opening the file in binary mode?
2. reading binary is not like ascii file reading.. because you can have a int or a char or a long type in binary file and hence you need to read accordingly and make proper values.
3. rather than using unsigned int, use unsigned char type. that will fit your needs. because you will be reading one full byte at one time and will put each byte in an array after that manipulate it. but yes you can directly read the exact your values also.
lets say first you want to read an int then you do this:
1 2
|
int var;
fread(&var, sizeof(int), 1, fp);
|
to read a char you do this:
1 2
|
char var;
fread(&var, sizeof(char), 1, fp);
|
so you can see the calls are changing with needs.
talking about the use of unsigned char, how you can use it..
lets say your binary file has five bytes, first an int type and then a char type.
so you do this:
1 2 3 4 5
|
uchar var[100];
fread(&var[0], sizeof(unsigned char), 5, fp);
int i = var[3] << 24 | var[2] << 16 | var[1] << 8 | var[0];
char c = var[4];
|
this is not an exact code but you can understand what you want.
4. then as kbw said about platform dependent files.. they might be big endian or small endian files.. though this case doesnt seem to be related to your problem.
rather than guessing your problem, it will be better if you post your problem code here.