I'm trying to write a simple program to generate hex output from file. This is my two.c file:
#include <stdio.h>
int
main(void) {
printf("%s\n", "Hello");
return 0;
}
which was compiled in this way:
gcc -std=c99 -Wall -Wextra -pedantic-errors two.c -o two
so I have two executable file.
Now, I wrote a program to read that file and show the binary (hexadecimal) output of it. This is my program:
#include <stdio.h>
int
fileSize(FILE *ptr) {
int size = 0;
fseek(ptr, 0, SEEK_END);
size = ftell(ptr);
fseek(ptr, 0, SEEK_SET);
return size;
}
int
main(void) {
FILE *fp;
fp = fopen("two", "rb");
int sz = fileSize(fp);
char buff[ sz ];
if (!fp)
printf("%s\n", "Not great at all");
else
while (!feof(fp)) {
fgets(buff, sz, fp);
for (int i = 0; i < sz; i++) {
printf("%02x%02x ", (buff[i] & 0xFF), (buff[i+1] & 0xFF));
if (!(i % 8))
printf("\n");
}
printf("\n");
}
fclose(fp);
}
And here is huge output of it http://pastebin.com/RVLy6H9B
The problem is, when I use linux command xxd two > two.hex I'm getting completely different output (it's not about formatting) and there is only about 500 lines of bytes, not about 8k like in my output.
xxd output: http://pastebin.com/Gw9wg93g
Where is the problem? Is something wrong with reading function fgets(buff, sz, fp);?