Fseek没有到达编译文件的末尾

时间:2014-11-30 23:58:46

标签: c file character fseek compiled

我试图计算已编译文件中的字符,但我无法到达最后一个字符。 这是我的测试代码,其中SEEK_END在某一点停止(我不知道为什么),其余文件仍然未读。

源代码是使用make编译的。
您可以使用GHex查看/编辑已编译的文件。
我的例子:
    - 原始编译文件:https://gist.github.com/anonymous/58aa85dac60047f4adeb
    - 在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);
        }

    }
}

谢谢! :)

0 个答案:

没有答案