I am working with a project which will use bitset. As the text file provided is very large(>800M), to load it directly to std::bitset will cost more then 25 seconds. So I want to preprocess the text file to a memory dumped binary file. Because a char with 8 bit will covert to 1 bit, the cost time of file load will reduce much. I write a demo code:
#include <iostream>
#include <bitset>
#include <string>
#include <stdexcept>
#include <fstream>
#include <math.h>
int main () {
const int MAX_SIZE = 19;
try {
std::string line = "1001111010011101011";
int copy_bypes = (int)ceil((float)MAX_SIZE / 8.0);
std::bitset<MAX_SIZE>* foo = new (std::nothrow)std::bitset<MAX_SIZE>(line); // foo: 0000
std::ofstream os ("data.dat", std::ios::binary);
os.write((const char*)&foo, copy_bypes);
os.close();
std::bitset<MAX_SIZE>* foo2 = new (std::nothrow)std::bitset<MAX_SIZE>();
std::ifstream input("data.dat",std::ios::binary);
input.read((char*)&foo2, copy_bypes);
input.close();
for (int i = foo2->size() -1 ; i >=0 ; --i) {
std::cout << (*foo2)[i];
}
std::cout <<std::endl;
}
catch (const std::invalid_argument& ia) {
std::cerr << "Invalid argument: " << ia.what() << '\n';
}
return 0;
}
it seems work fine, but I am worried this usage can really work fine in production enviroment.
Thanks in some advanced.