linux 文件切分与合并 lseek,linux系统编程之文件与IO(三):利用lseek()创建空洞文件...

一、lseek()系统调用 功能说明: 通过指定相对于开始位置、当前位置或末尾位置的字节数来重定位 curp,这取决于 lseek() 函数中指定的位置 函数原型: #include sys/types.h #include unistd.h off_t lseek(int fd, off_t offset, int whence); 参数说明:

一、lseek()系统调用

功能说明:

通过指定相对于开始位置、当前位置或末尾位置的字节数来重定位 curp,这取决于 lseek() 函数中指定的位置

函数原型:

#include

#include

off_t lseek(int fd, off_t offset, int whence);

参数说明:

fd:文件描述符

offset:偏移量,虚拟主机,虚拟主机,该值可正可负,负值为向前移

whence:搜索的起始位置,有三个选项:

(1).SEEK_SET: 当前位置为文件的开头,新位置为偏移量大小

(2).SEEK_CUR: 当前位置为文件指针位置,美国服务器,新位置为当前位置加上偏移量大小

(3).SEEK_END: 当前位置为文件结尾,新位置为偏移量大小

返回值:文件新的偏移值

二、利用lseek()产生空洞文件(hole)

说明:

The lseek() function allows the file offset to be set beyond the end of the file (but this does not change the size of the file).  If  data  is later written at this point, subsequent  reads of the data in the gap (a "hole") return null bytes ('\0') until data is  actually  written  into the gap.

程序代码:

#include #include#include#include

#define ERR_EXIT(m) \

do\

{ \

perror(m); \

exit(EXIT_FAILURE); \

}while(0)int main(void)

{intfd;intret;

fd= open(,O_WRONLY|O_CREAT|O_TRUNC,0644);if(fd == -1)

ERR_EXIT();

write(fd,,5);

ret= lseek(fd,1024*1024*1024,SEEK_CUR);if(ret == -1)

ERR_EXIT();

write(fd,,5);

close(fd);return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值