#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<string.h>
//文件IO lseek
//从文件开头开始定位,把相应内容替换
//从文件末尾开始定位,把相应内容替换
int main(int argc,char *argv[])
{
int fd;
if(argc != 2)
{
printf("usage %s <filename>\n",argv[0]);
exit(1);
}
fd = open(argv[1],O_RDWR | O_CREAT,0664);
if(fd < 0)
{
perror("fail to open");
exit(1);
}
#if 0
//从文件开头开始定位,把相应内容替换
lseek(fd,100,SEEK_SET);//文件开始位置向前移动100字节
write(fd,"hello",strlen("hello"));//在这个位置插入hello
#endif
//从文件末尾开始定位,把相应内容替换
lseek(fd,-100,SEEK_END);
write(fd,"hello",strlen("hello"));
return 0;
}
文件IO lseek
最新推荐文章于 2025-04-15 14:59:13 发布