This code demonstrates how to serialize and deserialize an ArrayXf object to/from a file.
This code demonstrates how to serialize and deserialize an ArrayXf object to/from a file.The code creates a 2D array of size 4x2, fills it with float values and writes it to a file. It then reads the data back and prints the content of the array.
#include <iostream>
#include <boost/filesystem.hpp>
#include <iolink/serialization/Serialization.h>
#include <iolink/storage/StreamAccessFactory.h>
static void
serializeArrayXf()
{
const boost::filesystem::path tmpFilePath = boost::filesystem::temp_directory_path() / "serialization_array_cpp.txt";
std::cout << "Serialize array into " << tmpFilePath << std::endl;
std::shared_ptr<StreamAccess> dst = StreamAccessFactory::openFileWrite(tmpFilePath.string());
if (!dst)
{
std::cerr << "Impossible to open file " << tmpFilePath.string() << std::endl;
}
Serialization::encodeArrayXf(arrayf, dst);
dst.reset();
std::shared_ptr<StreamAccess> src = StreamAccessFactory::openFileRead(tmpFilePath.string());
if (!src)
{
std::cerr << "Impossible to open file " << tmpFilePath.string() << std::endl;
}
ArrayXf arrayRead = Serialization::decodeArrayXf(src);
std::cout <<
"Content of unserialized array:" << arrayRead.
toString() << std::endl;
}
int
main(int argc, char** argv)
{
serializeArrayXf();
return EXIT_SUCCESS;
}
A multi dimensionnal array.
Definition ArrayX.h:31
void setAt(const VectorXu64 &index, T value)
Set a value to an element of the array.
std::string toString() const
Return a string representation.
A dynamically sized arithmetic vector.
Definition VectorX.h:18
All IOLink symbols are enclosed in this namespace.
Definition ArrayX.h:8