Originally Posted by
iMalc
The only thing that causes this is casting the value to a signed char and then casting back to a larger integer type. You should look for code which is doing that.
Note that you've only shown 32-bits here. I assume the other 32-bits are all one or all zero?
iMalc you are right I am casting char to a larger int. I was hoping I can do this
Code:
FILE *fp;
fp = fopen("c:\\test.tst","wb");
if(fp) {
int pos = 500;
char buf[8];
fwrite(&pos, sizeof(int), 1, fp);
fclose(fp);
fp = fopen("c:\\test.tst", "rb");
if(fp) {
fread(&pos, sizeof(int), 1, fp); // Put the val to pos - works
fseek(fp, 0, SEEK_SET); // Put the fp pos at start
fread(&buf, sizeof(int), 1, fp); // Put the val to buf
printf("%d : %d\n", pos, (int)buf); // (int)buf) will print junk
}
}
Just read the int from file to a char buf then just cast it to int but I guess there is no way to do that. This is just a portion of what I am trying to do. buf[] can have different data types (char, short, int) and I will just cast the buf at the right offset like
pos = (int)buf[4];
st = (char)buf[5];
is there any way to do this?