對大文件進行分塊處理,這裏只是簡單的順序執行 ios
可考慮實現併發分塊,經過文件指針的位置及偏移來實現 併發
#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; }