I'm trying to count the characters in a compiled file and i can't reach the last character. Here is my testing code, where SEEK_END stops at a certain point( i don't know why ) and the rest of file remains unread.
The source code was compiled using make.
You can use GHex to view/edit compiled file.
My example:
- Original compiled file: https://gist.github.com/anonymous/58aa85dac60047f4adeb
- Output before SEEK_END: https://gist.github.com/anonymous/6b1ab0b2dc3589a4d910
unsigned char *buffer = (unsigned char*) malloc(sizeof (unsigned char));
fseek(f, 0, SEEK_SET); // move the file indicator to first char
fseek(f, 0, SEEK_END); // move the file indicator to the end of file
long ss = ftell(f); // get number of bytes
fseek(f, 0, SEEK_SET); // move the file indicator to first char
int run = 1;
long p = 0;
while (run) {
fseek(f, 0, SEEK_CUR); // indicator to the next char
fread(buffer, 1, 1, f); // put value into buffer
p = ftell(f); // check the length of bytes
if (p == ss - 1) { // if is the end of file
FILE *f3; // opened the file again
f3 = fopen(filename, "rb");
fseek(f3, ss-10, SEEK_CUR); // move the indicator to the ss-10
int jj = 1;
for (jj; jj < 400; jj++) { // read after the SEEK_END of file but nothing happens
fseek(f3, 0, SEEK_CUR);
fread(buffer, 1, 1, f3);
ferror(f3);
}
}
}
Thanks ! :)