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

来源:互联网 发布:医疗大数据公司 难产 编辑:程序博客网 时间:2024/06/06 07:15

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;}


0 0
原创粉丝点击