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
- if use open() open a file with flags O_APPEND,you may not reset the filesize with lseek()+write().
- open+errno+write+lseek
- Can not open file with "ios_base::in | ios_base::app"
- How to open and sort a CSV file with Ruby
- open with the a CHMmaker (椰书制造)
- NAND write: '$filesize' is not a number
- linux open read write lseek close
- Qt5 Crash When Open File With QFileDialog
- The 10 Best Open Source Projects You Should Be Volunteering To Help With
- Android studio Error:(1, 0) Plugin with id 'com.Android.application' not found.Open File
- "The file you are trying to open, '[filename]', is in a different format" Excel Error
- NTHU OJ 1015 - May the Right Force with you-图论
- APUE习题:以O_APPEND标志open的文件,lseek后读写问题
- APUE-文件与目录:以O_APPEND标志open的文件,lseek后读写问题
- 用O_APPEND标志open一个文件,能否用lseek在任意位置读写
- How to change the name in open with list / File association.
- java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the
- Java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the
- 传感器的使用
- Action访问web资源的方式
- PHP基本语法和数据类型
- Bootstrap动态切换选项卡
- 【前端之路第四天】含义篇——模块开发,同步加载,异步加载
- if use open() open a file with flags O_APPEND,you may not reset the filesize with lseek()+write().
- JAVA 字节流使用总结
- 第一次使用git配置
- 输入输出函数
- 字符串正则查找替换
- wordpress 在文章内容页获取相关文章,本文章所在分类的其它文章的方法
- 现货白银骗局!!我的经历!!希望大家不要上当...
- C#整数三种强制类型转换int、Convert.ToInt32()、int.Parse()的区别
- Xfermode的正确用法