c++ 文件分块

对大文件进行分块处理,这里只是简单的顺序执行

可考虑实现并发分块,通过文件指针的位置及偏移来实现

#include <iostream> 
#include <fstream> 
#include <sstream> 
#include <string> 


#define KB_4 4096 
#define KB_8 8192 
#define KB_16 16384 
#define KB_32 32768 
#define KB_64 65536L 
#define KB_128  131072L 
#define KB_256 262144L 
#define KB_512 524288L 
#define MB_1 1048576L 
#define MB_2 2097152L 
#define MB_4 4194304L 


using namespace std; 


string int_to_str(int i) 
{ 
stringstream ss; 
ss << i ; 
return ss.str(); 
} 


bool write_file(char * block, int length, const string & filename) 
{ 
ofstream ofs(filename.c_str()); 
if(!ofs.is_open()) 
{ 
cout << "Failed to open the file!" << endl; 
return false; 
} 

ofs.write(block, length); 


ofs.close(); 
return true; 
} 


void test() 
{ 

const int size = MB_2; 
ifstream ifs("1GB"); 
if(!ifs.is_open()) 
{ 
cout << "Failed to open the file!" << endl; 
return ; 
} 


string block_id = ""; 
string head = "2MB/"; 

char block[size]; 

int i = 1; 
do 
{ 
block_id = head + int_to_str(i++); 
if(ifs.read(block, size) == 0) 
break; 

if(!write_file(block, size, block_id)) 
{ 
cout << "error!" << endl; 
break; 
} 
}while(!ifs.eof()); 
ifs.close(); 
cout << "over!" << endl; 
}


转载于:https://my.oschina.net/u/566591/blog/162526

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值