c - fseek 不适用于空文件

标签 c fseek

我有以下代码:

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。有谁知道为什么没有发生这种情况?

最佳答案

来自the documentation of fseek

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/

相关文章:

c - 如何多播(发送)到第一个网卡?

c - 生成随机数

圆形数组实现

c - fseek - 无法跳过大量字节?

c - 无法从当前位置向后使用 fseek()

c - 声明与定义的联系依据

c - arm-none-eabi-gcc 是否支持#pragma pack(n)

c - 随机写入输出文件的顺序?

c - fgetpos/fsetpos 比 fseek 快吗?

c - fread() 返回零字节读取 : I Don't Understand Why