Skip to content

Instantly share code, notes, and snippets.

@sandaru1
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandaru1/ba8a1318cbae158ae743 to your computer and use it in GitHub Desktop.
Save sandaru1/ba8a1318cbae158ae743 to your computer and use it in GitHub Desktop.
#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