Last active
August 29, 2015 14:14
-
-
Save sandaru1/ba8a1318cbae158ae743 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <sstream> | |
using namespace std; | |
#define SPLIT_SIZE 1024 * 1024 * 1024 | |
int main (int argc, char *argv[]) { | |
if (argc<2) { | |
cout << "Usage : " << argv[0] << " [filename]" << endl; | |
return 0; | |
} | |
streampos size; | |
char * memblock; | |
int filenumber = 0; | |
ifstream file (argv[1], ios::in|ios::binary); | |
if (file.is_open()) | |
{ | |
long long position = 0; | |
long long current_size = 0; | |
std::ostringstream out_filename; | |
out_filename << filenumber << "." << argv[1]; | |
ofstream outfile(out_filename.str().c_str(),ios::out|ios::binary); | |
while (true) { | |
int size; | |
file.read((char*)&size,sizeof(int)); | |
if (file.eof()) { | |
if (current_size > 0) | |
cout << current_size << endl; | |
return 0; | |
} | |
if (current_size + size > SPLIT_SIZE) { | |
cout << current_size << endl; | |
current_size = 0; | |
filenumber++; | |
out_filename.str(""); | |
out_filename.clear(); | |
out_filename << filenumber << "." << argv[1]; | |
outfile.close(); | |
outfile.open(out_filename.str().c_str(),ios::out|ios::binary); | |
} | |
outfile.write((char*)&size,4); | |
char* buffer = new char[size-4]; | |
file.read(buffer,size-4); | |
outfile.write(buffer,size-4); | |
delete buffer; | |
position += size; | |
current_size += size; | |
} | |
file.close(); | |
} else cout << "Unable to open file"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment