July 6th, 2012, 11:08 AM
Efficient way to write huge boost dynamic_bitset vector to a file and read it back
I have a huge vector of boost dynamic_bitset. I want to write the dynamic_bitset vector to a file and later read the file back into a dynamic_bitset vector. Is the memory for dynamic_bitset allocated as a contiguous block of memory (so that I can write the entire vector at once without traversing) ?
The size of the bitset vector is in order of millions. So I am looking for an efficient way to write them to a file instead of iterating through the elements.
I converted the dynamic_bitset to a string and then wrote the string to a file. Later read the string from the file and converted it back to dynamic_bitset.
Below is the code I wrote in C++ using Visual Studio:
But even this method, storing it as a string, takes a long time to write to the file. And when I try to read back from the file into the string, I get an "unhandled access violation exception". Is there a more efficient way to implement the same?
using namespace std;
int main(int argc, char* argv)
// Initializing a bitset vector to 0s
bit_vector = 1; bit_vector = 1; bit_vector = 1; bit_vector = 1; bit_vector = 1;
cout<<"Input :"<<bit_vector<<endl; //Prints in reverse order
//Converting dynamic_bitset to a string
//Writing the string to a file
ofstream out("file", ios::out | ios::binary);
char *c_buffer = (char*)buffer.c_str();
//Find length of the string and reading from the file
int len = strlen(c_buffer);
char* c_bit_vector = new char(len+1);
c_bit_vector[len] = 0;
//Converting string back to dynamic_bitset
string str2 = c_bit_vector;
boost::dynamic_bitset<> output_bit_vector( str2 );
Tags for this Thread
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.