if use open() open a file with flags O_APPEND,you may not reset the filesize with lseek()+write().

本文通过两个示例代码对比展示了在使用O_APPEND标志打开文件时,如何影响lseek()+write()组合操作来调整文件大小。当不使用O_APPEND标志时,可以通过lseek()+write()将文件指针定位到特定位置并写入数据来调整文件大小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1:open a file with flags O_APPEND,you may can not reset filesize with lseek()+write().

code1:


#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
        int ret=0;
        int fd=-1;
        struct stat sb;
        fd = open("test.txt",O_RDWR|O_APPEND|O_CREAT|O_TRUNC,00777);//flags with O_APPEND
        if(fd==-1)
        {
                perror("open() error:");
        }
        ret = lseek(fd,0,SEEK_END);
        if(ret==-1)
        {
                perror("lseek() error:");
        }
        printf("before lseek() to set offset of file,the end_filesize=%d\n",ret);//ret==0

        ret=lseek(fd,100,SEEK_SET);//set offset of file with lseek()
        if(ret==-1)
        {
                perror("lseek()2 error():");
        }

        ret=lseek(fd,0,SEEK_CUR);
        if(ret==-1)
        {
                perror("lseek()3 error:");
        }
        printf("after set offset with lseek(),before write 1 byte data,cur_offset off the file is %d\n",ret);//ret==100

        ret=write(fd,"",1);//write '\0' to the file.
        if(ret==-1)
        {
                perror("write() error:");
        }

        ret=lseek(fd,0,SEEK_CUR);
        if(ret==-1)

        {
                perror("lseek()3 error:");
        }
        printf("after write 1 byte data to the file,cur_offset of the file is %d\n",ret);//ret==1;

        ret=fstat(fd,&sb);
        if(ret==-1)
        {
                perror("fstat error:");
        }
        printf("filesize = %ld\n",sb.st_size);//filesize == 1;

        close(fd);

return 0;
}

1:open a file without flags O_APPEND,you can reset filesize with lseek()+write().

code2:

#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
        int ret=0;
        int fd=-1;
        struct stat sb;
        fd = open("test.txt",O_RDWR|O_CREAT|O_TRUNC,00777);//flags with O_APPEND
        if(fd==-1)
        {
                perror("open() error:");
        }
        ret = lseek(fd,0,SEEK_END);
        if(ret==-1)
        {
                perror("lseek() error:");
        }
        printf("before lseek() to set offset of file,the end_filesize=%d\n",ret);//ret==0

        ret=lseek(fd,100,SEEK_SET);//set offset of file with lseek()
        if(ret==-1)
        {
                perror("lseek()2 error():");
        }

        ret=lseek(fd,0,SEEK_CUR);
        if(ret==-1)
        {
                perror("lseek()3 error:");
        }
        printf("after set offset with lseek(),before write 1 byte data,cur_offset off the file is %d\n",ret);//ret==100

        ret=write(fd,"",1);//write '\0' to the file.
        if(ret==-1)
        {
                perror("write() error:");
        }

        ret=lseek(fd,0,SEEK_CUR);
        if(ret==-1)

        {
                perror("lseek()3 error:");
        }
        printf("after write 1 byte data to the file,cur_offset of the file is %d\n",ret);//ret==101;

        ret=fstat(fd,&sb);
        if(ret==-1)
        {
                perror("fstat error:");
        }
        printf("filesize = %ld\n",sb.st_size);//filesize == 101;

        close(fd);

return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值