红联Linux门户
Linux帮助

Linux创建一个空洞文件

发布时间:2017-03-08 09:19:50来源:linux网站作者:字远尘
在使用迅雷下载的时候,查看迅雷新建的文件发现该文件大小和要下载的文件一样大。
其实迅雷事先在本地创建了一个与要下载一样大的临时文件,然后后面通过多线程方式。
从网络上下载这个文件,一点点填入那个临时文件。
 
创建空洞文件代码如下:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#define N_M 10*1024*1024
int main(int argc, char **argv)
{
if ( argc != 2 )
{
printf("usage: %s file_name\n", argv[0]);
return 0;
}
int fd = open(argv[1], O_RDWR);
if ( !fd )
{
perror("open");
return -1;
}
if ( lseek(fd, N_M, SEEK_SET) < 0 )
{
perror("lseek");
return -1;
}
write(fd, "\0", 1);
close(fd);
return 0;
}
 
本文永久更新地址:http://www.linuxdiyf.com/linux/28991.html