0

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 ! :)

Alex R
  • 1
  • 1
  • I don't understand what you are trying to do but there are several issues with the code. fseek(f, sizeof(unsigned char), SEEK_END) will set the file position to one byte after the end of file. So ss will be the file size plus 1. fseek(f, 0, SEEK_CUR) will do absolutely nothing at all. The call to ferror() is only necessary if you actually are interested in the return value. – Lars Ljung Dec 01 '14 at 11:38
  • You are right, i've made a mistake by moving to the end +1. The point is that i can't read the whole compiled file to the end of it. The SEEK_END indicates the end of file wrong, there are plenty characters left to read. Look at those 2 examples. – Alex R Dec 01 '14 at 12:10

0 Answers0