Reputation: 39
I am trying to save a
vector<vector<int>>
as an xml file. the vectors will be dynamically resizable . I tried using BOOST:SERIALIZATION but I receive this error. libc++abi.dylib: terminating with uncaught exception of type boost::archive::archive_exception: input stream error
I can't see any problems with my code. Another method other than boost to save as an xml would be fine with me if someone can suggest one.
#include <iostream>
#include <vector>
#include <iostream>
#include <fstream>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
using namespace std;
int main() {
using namespace std;
int numChords;
vector<vector<int> > chords; // array to store chord arrays
numChords = 2; // size of outer array
chords = {{41, 48, 55, 56, 58, 63},
{44, 51, 56, 58, 61, 63}}; // data structure
// print the array
cout << endl << "you entered: " << endl;
// print the results
for (int i = 0; i < numChords; i++) {
for (int j = 0; j < chords[i].size(); j++) {
cout << chords[i][j] << ",";
}
cout << endl;
}
// store the array to a file
std::ofstream ofs("dump.dat");
boost::archive::text_oarchive oa(ofs);
oa & chords;
chords.clear(); // clear the original array
// restore the array from the file
std::ifstream ifs("dump.dat");
boost::archive::text_iarchive ia(ifs);
ia & chords;
cout << endl << "you saved: " << endl;
// print the restored array
for (int i = 0; i < numChords; i++) {
for (int j = 0; j < chords[i].size(); j++) {
cout << chords[i][j] << ",";
}
cout << endl;
}
return 0;
}
I tried various different filenames and filepaths. I tried using & or << >> after the boost statement.
Full output is
you entered:
41,48,55,56,58,63,
44,51,56,58,61,63,
libc++abi.dylib: terminating with uncaught exception of type boost::archive::archive_exception: input stream error
Process finished with exit code 6
Thanks in advance for any advice.
Sean
Upvotes: 0
Views: 323
Reputation: 20969
Enclose output and input operations on stream by braces:
{ //<--
// store the array to a file
std::ofstream ofs("dump.dat");
boost::archive::text_oarchive oa(ofs);
oa & chords;
}
chords.clear(); // clear the original array
{ // <--
// restore the array from the file
std::ifstream ifs("dump.dat");
boost::archive::text_iarchive ia(ifs);
ia & chords;
}
Here is reference about exceptions which are thrown by boost::archive.
Below sentence regards to input/output stream error
:
Be sure that an output archive on a stream is destroyed before opening an input archive on that same stream.
If this condition is not fulfilled, you get exception.
Upvotes: 1