C Fseek没有';t到达已编译文件的结尾

C Fseek没有';t到达已编译文件的结尾,c,file,character,fseek,compiled,C,File,Character,Fseek,Compiled,我试图计算编译文件中的字符数,但我无法找到最后一个字符。 这是我的测试代码,其中SEEK_END在某个点停止(我不知道为什么),文件的其余部分保持未读状态 源代码是使用make编译的 您可以使用GHex查看/编辑已编译的文件 我的例子: -原始编译文件: -搜索结束前的输出: unsigned char*buffer=(unsigned char*)malloc(sizeof(unsigned char)); fseek(f,0,SEEK_集);//将文件指示符移到第一个字符 fseek(f,0

我试图计算编译文件中的字符数,但我无法找到最后一个字符。 这是我的测试代码,其中SEEK_END在某个点停止(我不知道为什么),文件的其余部分保持未读状态

源代码是使用make编译的
您可以使用GHex查看/编辑已编译的文件
我的例子:
-原始编译文件:
-搜索结束前的输出:

unsigned char*buffer=(unsigned char*)malloc(sizeof(unsigned char));
fseek(f,0,SEEK_集);//将文件指示符移到第一个字符
fseek(f,0,SEEK_END);//将文件指示器移到文件末尾
长ss=ftell(f);//获取字节数
fseek(f,0,SEEK_集);//将文件指示符移到第一个字符
int run=1;
长p=0;
while(运行){
fseek(f,0,SEEK_CUR);//指向下一个字符的指示符
fread(buffer,1,1,f);//将值放入缓冲区
p=ftell(f);//检查字节的长度
if(p==ss-1){//if是文件的结尾
FILE*f3;//再次打开该文件
f3=fopen(文件名,“rb”);
fseek(f3,ss-10,SEEK_CUR);//将指示器移到ss-10
int jj=1;
对于(jj;jj<400;jj++){//在文件的SEEK_结尾后读取,但什么也没有发生
fseek(f3,0,SEEK_CUR);
fread(缓冲器,1,1,f3);
ferror(f3);
}
}
}

谢谢!:)

我不明白你想做什么,但是代码有几个问题。fseek(f,sizeof(unsigned char),SEEK_END)将文件位置设置为文件结束后的一个字节。因此ss将是文件大小加1。fseek(f,0,SEEK_CUR)绝对不会做任何事情。只有当您确实对返回值感兴趣时,才需要调用ferror()。您是对的,我犯了一个错误,移到了+1结尾。关键是我不能把整个编译文件读到最后。SEEK_END表示文件结尾错误,还有很多字符需要读取。看看这两个例子。
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);
        }

    }
}