I have problem with reading a binary file in C, the problem is: when i read the file it will not read the complete file but left some of the data from last…
the code used to read the file is as below:
FILE *file;
unsigned long fileLen;
//Open file
file = fopen("ASELogo.png", "rb");
if (!file)
{
fprintf(stderr, "Unable to open file %s", name);
}
fseek(file, 0, SEEK_END);
fileLen=ftell(file);
fseek(file, 0, SEEK_SET);
buffer=(char *)malloc((fileLen+1)*sizeof(unsigned char));
fread(buffer, fileLen, 1, file);
for(int i = 0;i < fileLen;++i)
printf("%c", ((char *)buffer)[i]);
And when m printing the file it will not print the whole contents of the file…
the actual file contents are:
âPNG
You’re printing out bytes as characters. That’s fine if they happen to be ASCII range, but you’re going to end up printing control characters and so on.
Try formatting the characters as bytes (e.g. 0xFF and 0x12) and see if that helps.