'Use fread() to read binary file contents
I want to use fread to read a segment of a binary file. The binary content is 0xf3 0xf 0x1e 0xfa...
But I read and output from fread() 0xfffffff3 0xf... I want to know why 0xf3 becomes 0xfffffff3 0xf 0x1e 0xfffffffa...
My implementation code is as follows:
char movslq_inst[length];
/* Open the target_binary */
FILE * target_binary = fopen(target_path,"rb+");
/* Jump to target address */
if (fseek(target_binary, offset, SEEK_SET) != 0)
{
printf("Unable to seek file '%s' at offset '%i", target_path, offset);
exit(0);
}
/* Read the target bytes */
if (fread(movslq_inst, length, 1, target_binary) != 1)
{
printf("Unable to read file '%s'", target_path);
exit(0);
}
/* test examine */
printf("read content: \n");
for(int i = 0; i < length; i++)
{
printf("0x%x ",movslq_inst[i]);
}
printf("\n");
Solution 1:[1]
I worked it out. It should be unsigned char movslq_inst[length]; instead of
char movslq_inst[length];.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |


