if use open() open a file with flags O_APPEND,you may not reset the filesize with lseek()+write().
2016-10-19 23:15
429 查看
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;
}
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;
}
相关文章推荐
- 关于“Windows cannot access the specified device, path, or file. You may not have the appropriate permissions to access the item.”问题的解决
- OpenAirInterface USRP安装时You do not have write permissions at the install location问题解决方案
- A class file was not written. The project may be inconsistent, if so try refreshing this project and building it
- python csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
- A class file was not written. The project may be inconsistent, if so try refreshing this project and building it. eclipse提示错误
- 编译错误:A class file was not written. The project may be inconsistent, if so try refreshing this projec
- 偶遇-Two Filesystems with the same UUID have been detected. Make sure you do not have two ESXi installations
- python读取csv文件是报错_csv.Error: iterator should return strings, not bytes (did you open the file in text
- Error: iterator should return strings, not bytes (did you open the file in text mode?)
- A class file was not written. The project may be inconsistent, if so try refreshing this project and building it. eclipse提示错误
- 使用cocapods报错 [!] Your Podfile has had smart quotes sanitised. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes
- Recover a file even if it was not committed but it has to have been added when you use git reset head by mistake.
- A class file was not written. The project may be inconsistent, if so try refreshing this project and
- how to solve the problem when you have chmod the directory to 777 but the apache still can not write file
- retext出现Could not parse file contents, check if you have the necessary module installed解决方案
- anaconda2下面安装opencv2.4.13.4完成----解决默认安装的问题----Thefunction is not implemented. Rebuild the library with Windows, GTK+ 2.x orCarbon support. If you are on Ubuntu or Debian, install libgtk2.0‑dev and pkg
- Redis无法启动You may fix this problem by either reducing the size of the Redis heap with the --maxheap
- Sharepoint Error : The URL "filename" is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web
- Program for Android in C/C++ with the Native Development Kit (if you dare)
- 解决cakePHP中的. session_start()[function.session-start];open_basedir restnction in effect.File(c:/windows/temp/)is not within the a