sparse file

[size=medium]
windows - chsize
unix - truncate
unix - lseek

二进制,填充0

chsize/truncate比write快很多很多

空洞文件 又叫 稀疏文件 Sparse File

[url]http://space.itpub.net/8242091/viewspace-619756[/url]
[url]http://hi.baidu.com/holylizejin/blog/item/5c8072885f67be739f2fb420.html[/url]
[url]http://blog.chinaunix.net/u3/104183/showart_2277274.html[/url]
[url]http://blog.youkuaiyun.com/BABY313/archive/2010/01/18/5208836.aspx[/url]

linux 命令
ls -l file.txt   // 文件大小
du -sk file.txt   // 文件实际分配的空间
fileplace file.txt {必须安装perfagent.tools包才能使用}


[/size]




#include <iostream>
#include <windows.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

using namespace std;

int main(int argc,char argv[])
{
char buf[] = "0123456789";

int fd = open("G:/data", O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
if (-1 == fd) return 0;

cout << write(fd, buf, sizeof(buf)) << endl;

chsize(fd, 20000);

close(fd);
cout << GetLastError() << endl;
return 0;
}



#include <stdio.h>
#include <fcntl.h>
#include <string.h>

#define MAXSIZE 1024

int main(int argc, char *argv[])
{
int fd = open("file.txt",O_RDWR);
printf("fd = %d \n",fd);

char buf[MAXSIZE];
memset(buf, 0, MAXSIZE);
int size = read(fd,buf ,MAXSIZE);
printf("read size = %d \n",size);
printf("conten = %s \n",buf);

int lse = lseek(fd, 4096, SEEK_END);
printf("lseek size= %d \n",lse);

char s[] = "0123";
int ok = write(fd, s, sizeof(s));
printf("write size = %d \n",ok);

close(fd);

return 0;
}


int createSparseFile(char *path, long size)
{
if (access(path, F_OK) == 0) return -1;

int fd = creat(path, O_WRONLY);
if (fd < 0) return -2;

lseek(fd, size-1, SEEK_SET);
write(fd, 0, 1);
close(fd);

return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值