温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-06-19 17:32:17 来源:网络 阅读:583 作者:银河星君 栏目:编程语言

/*

  • 当使用lseek函数定位到超出文件尾端之后,对于新写入的数据需要分配磁盘块,但是对于原文件
  • 尾端和新开始写位置之间的部分则不需要分配磁盘块,这会产生空洞文件,文件中的空洞并不要求
  • 在磁盘上占有存储区,具体处理方式与文件系统的实现有关,请尝试使用open、lseek等函数创建
  • 一个含有空洞的文件。
    */
    #include <sys/types.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <string.h>

#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;
}

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI