I am having a vector whose size can be really big (1 million elements). I am wrote the contents of the vector to a file as byte values. I am not able to figure out how I can read the byte values back into the vector.
Here is the code:
#include <fstream>
#include <vector>
#include <iterator>
#include <iostream>
using namespace std;
int main()
{
// Filling a vector with values
std::vector<bool> ve;
ve.push_back(true);
ve.push_back(false);
ve.push_back(true);
ve.push_back(false);
ve.push_back(true);
// Printing the values of the vector
for(unsigned int i = 0; i < ve.size(); i++)
cout << ve.at(i) << ".";
cout << endl;
// Writing the vector contents to a file
const char* file_name = "abc.txt";
ofstream outfile(file_name, ios::out | ios::binary);
outfile.write((const char*)&(ve[0]), ve.size());
outfile.close();
// Reading the file and filling the vector with values
ifstream infile ("abc.txt", ifstream::binary);
vector<bool> out_ve((std::istreambuf_iterator<char>(infile)),
std::istreambuf_iterator<char>());
while( !infile.eof() )
out_ve.push_back(infile.get());
// Checking if the values read are the same as the original values
cout << "SIZE: " << out_ve.size() << endl;
for(unsigned int i = 0; i < out_ve.size(); i++)
cout << out_ve.at(i) << ".";
cout << endl;
infile.close();
return 0;
}
[edit] Closed the file after writing and the output is very different from the input.
1.0.1.0.1.
SIZE: 6
1.1.1.0.1.1.
How can I get the correct elements into the vector out_ve?
Writing data from most STL containers cannot be done with
outfile.write((const char*)&(ve[0]), ve.size());because they manage their memory in complex ways that’s fundamental to the way they operate. Withvector, it works, because memory storage is contiguous, butvector<bool>is special because of the way it packs multiple bools into a single byte. As commenters have already pointed out,ve[0]returns a special temporary quasi-reference type, and writing that reference out by casting to achar*will produce something totally unrelated to the data that’s in the vector.Even if this construction gave you access to the raw memory of the vector, the code that you’re using to write out the data is incompatible with the code that you’re using to read in the data. The code that you’re using to write out the data would pack 8
boolentries into eachchar, but the code you’re using to read in the data converts eachcharinto a singlebool.Since you’re reading back your data using an
istreambuf_iterator, why not write it out the same way:This writes out one
boolper byte.If you want to write out data in a packed representation that writes one bit per
bool, I think you’ll need to invent your own input and output iterators.