Deconstructing your source code a bit:
void getInfo(FILE* inputFile)
{
char chunk[4];
int liIndex;
for (liIndex = 0 ; liIndex < 4 ; liIndex++)
{
fread(chunk, sizeof(char), 4, inputFile);
You're reading 4 bytes at a time, repeated 4 times; that means when the loop exits, you've read 16 bytes, and chunk contains bytes 12-16. Is that what you intended? If you just want to read the first four bytes, ditch the loop and just call fread
once. If you want to read and print the first 16 bytes, then you'll need to move the output statement into the loop as well.
As others have mentioned, don't hardcode the 4; use sizeof chunk
instead.
}
printf("\n chunk %s", chunk);
}
Remember that the %s
conversion specifier expects the corresponding argument to point to a 0-terminated sequence of printable characters, so chunk[0]
through chunk[2]
need to be printable (i.e., isprint(chunk[i]) == 1
), and chunk[3] had better always be a 0, or you could get some strange output. Since that tends to rarely be the case in binary files, you may want to change how you write that output.
Here's how you could print the individual bytes as integer values:
printf("chunk: ");
for (i = 0; i < sizeof chunk; i++)
printf("%02x ", chunk[i];
putchar('\n');