发表于 2019-1-28 07:41:51

请尝试使用open、lseek等函数创建 一个含有空洞的文件

  /*

[*]当使用lseek函数定位到超出文件尾端之后,对于新写入的数据需要分配磁盘块,但是对于原文件
[*]尾端和新开始写位置之间的部分则不需要分配磁盘块,这会产生空洞文件,文件中的空洞并不要求
[*]在磁盘上占有存储区,具体处理方式与文件系统的实现有关,请尝试使用open、lseek等函数创建
[*]一个含有空洞的文件。
*/
#include
#include
#include
#include
#include
#include
#include
  #define FLAGS O_WRONLY|O_CREAT|O_TRUNC/定义参数flags:以读写方式打开文件,向文件添加内容时从文件尾开始写/
#define MODE 0600                     /定义参数MODE:文件所有者读写方式/
#define FILENAME "/home/mrhe/test"       /要进行操作的文件/
  int main(int argc, char argv[])
{
int count;
int fd;
char buf1[]={"abcdefghij"};         //缓冲区1,长度为10
char buf2[]={"1234567890"};         //缓冲区2,长度为10
const char pathname=FILENAME;
if((fd=open(pathname,FLAGS,MODE))==-1)
{
printf("error,open file failed!\n");
exit(1);
}
count=strlen(buf1);
if(write(fd,buf1,count)!=count)   //调用write函数将缓冲区1的数据写入文件
{
printf("error,write file failed!\n");
exit(1);
}
if(lseek(fd,50,SEEK_SET)==-1)
{
printf("error,lseek failed!\n");
exit(1);
}
count=strlen(buf2);
if(write(fd,buf2,count)!=count)      //调用write函数将缓冲区2的数据写入文件
{
printf("error,write file failed!\n");
exit(1);
}
return 0;
}



页: [1]
查看完整版本: 请尝试使用open、lseek等函数创建 一个含有空洞的文件