#include <stdio.h>
int main()
{
int i,j;
FILE *f;
f=fopen("./pathto/sth.bmp","rb");
fread(&i,1, 1, f);
printf("%d ",i);
fread(&j,1, 1, f);
printf("%d ",j);
return 0;
}
I want to read the first 2 values from a bmp file. I know that they are 66 and 77 . The problem is that if i read only the first value the variable "i" becomes 66 which is good. But if i read the second value , as "j" , then "j" becomes 77 which is good , and "i" takes a random value something like 196540 and i don't understand why. So if i read the first value everything is ok. If i read the first 2 values, the last value is good, but the first modifies, it becomes a random one , sth like 196540
intvariables when you're readingbytesized values? What issizeof(int)on the platform you're using?unsigned char sig[2]viafread(sig, sizeof(sig), 1, f)would be nearer to what you seem to want, though you probably want to take the time to read the entire header carefully.fopen()andfread()to assure the operation(s) were successful.