我有以下代码:
int main(int argc,char *argv[]){
FILE *fp;
if(argc != 2){
perror("Please specify a file");
return 1;
}
if((fp=fopen(argv[1], "rb")) == 0){
perror("File not found!");
return 2;
}
int a = fseek(fp, 1000, SEEK_CUR);
if(a<0){
printf("fail\n");
}
fclose(fp);
return 0;
}
当我用空的二进制文件调用它时,没有发生错误,尽管我希望 fseek() 返回 -1。有谁知道为什么没有发生这种情况?
最佳答案
POSIX allows seeking beyond the existing end of file. If an output is performed after this seek, any read from the gap will return zero bytes. Where supported by the filesystem, this creates a sparse file.
关于c - fseek 不适用于空文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28398667/