实现此函数的基本思路:先提取 n 以前的数据段存入缓冲区,再提取 m 以后的数据段存入缓冲区,然后把缓冲区中的数据段重新写入文件,最后用 truncate 函数来截断文件。
需要注意的是,这里给的区间是一个左闭右开的区间,因此在文件内移动指针的时候要作相应处理,以完成删除 n 但不删除 m。
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int cut_file(const char *path,size_t n,size_t m)
{
if(n<0||m<n) return -1;
FILE *fp=fopen(path,"r+");
if(NULL==fp)
{
perror("fopen");
return -1;
}
char *first=malloc(n-1);
fread(first,1,n-1,fp);//把fp所指文件的n-1个字节存入缓冲区,前一段数据
if(!first)
{
perror("fread");
return -1;
}
fseek(fp,m-1,SEEK_SET);//因为左闭右开,所以把指针-1拿到缓冲区
struct stat statbuf;
stat(path,&statbuf);//获取文件属性
int range=statbuf.st_size-m;//存放后面一段数据的范围
char *second=malloc(range);
fread(second,1,range,fp);//存放后一段数据
if(!second)
{
perror("fread");
return -1;
}
//从头开始写入
rewind(fp);//把指针移到文件头部准备写入工作
fwrite(first,1,n-1,fp);//先写入前一段数据
fwrite(second,1,range,fp);//再写入后一段数据
truncate(path,n-1+range);//把文件截断
free(first);
free(second);
fclose(fp);
return 0;
}
int main(int argc,const char* argv[])
{
int n=2,m=4;//要删除的区间,为左闭右开,第2个要删除,第4个不删除
if(!cut_file("test.txt",n,m)) printf("cut success!");
else printf("cut fail");
return 0;
}
over