Simple way to store/read data from file in C++
An answer to this question on the Scientific Computing Stack Exchange.
Question
I've been running various simulations with C++, and doing so has often involved saving lots of data to file (real/complex matrices, arrays, etc) and then reading them into other programs later. However, I have been unable to figure out a consistent and simple way of doing so. My solutions often involve several dozen lines of difficult-to-read code for each different kind of data structure that I want to save/read from a file. Are there any known libraries that can do this, or at least aid in the process? If there are any sort of matrix libraries that allow automatic saving/reading of matrices/vectors to file, that would also be helpful (Eigen does not have this option, as far as I can tell).
Answer
Cereal is a straight-forward and easy-to-use serialization library for C++ data structures. It knows all of the STL data structures and adapting custom classes for use with it is easy.
Example code:
#include <cereal/types/unordered_map.hpp>
#include <cereal/types/memory.hpp>
#include <cereal/archives/binary.hpp>
#include <fstream>
struct MyRecord
{
uint8_t x, y;
float z;
template <class Archive>
void serialize( Archive & ar )
{
ar( x, y, z );
}
};
struct SomeData
{
int32_t id;
std::shared_ptr<std::unordered_map<uint32_t, MyRecord>> data;
template <class Archive>
void save( Archive & ar ) const
{
ar( data );
}
template <class Archive>
void load( Archive & ar )
{
static int32_t idGen = 0;
id = idGen++;
ar( data );
}
};
int main()
{
std::ofstream os("out.cereal", std::ios::binary);
cereal::BinaryOutputArchive archive( os );
SomeData myData;
archive( myData );
return 0;
}