write and read a 3d vector of struct in file C++

I want to save this struct and load it later:

struct Block
{
    vector<int> numbers;
    map<int , char> bits;
    map<string , string > states;
    string off;
}relationTable[7][11][13];

Is there any way except use of several for loops ? I use this function for save:

void saveData()
{
    string location = "test.bin";
    ofstream fs(location, std::ios::out | std::ios::binary | std::ios::app);
    fs.write(reinterpret_cast<const char *>(relationTable), sizeof(relationTable));
    fs.close();
}

And this one for load:

void loadData()
{
    string location = "test.bin";
    Block array[64][96][2];
    ifstream file (location, ios::in|ios::binary|ios::ate);
    if (file.is_open())
    {
        file.seekg (0, ios::beg);
        file.read ((char*)&array, sizeof(array));
        file.close();
    }

    ...//some use of array
}

But it didn’t work, because it only save the pointers! What should i do?

Answer

It’s not very clear what is relationTable for you. If you want to save your structure into a file, the easier way is to use boost::serialization see : How do you serialize an object in C++?

you can do something like this :

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/map.hpp>
include <boost/serialization/string.hpp>
#include <boost/serialization/vector.hpp>

namespace boost {
namespace serialization {

template<class Archive>
void serialize(Archive & ar, Block& b, const unsigned int version)
{
    ar & b.numbers;
    ar & b.bits;
    ar & b.states;
    ar & b.off;
}

void save_Block(const Block &b, const char * filename){
// make an archive
std::ofstream ofs(filename);
boost::archive::text_oarchive oa(ofs);
oa << b;
}
}}

Fully working sample program:

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/vector.hpp>

#include <fstream>

struct Block
{
    typedef std::vector<int> Numbers;
    typedef std::map<int, char> Bits;
    typedef std::map<std::string, std::string> States;

    Numbers numbers;
    Bits bits;
    States states;
    std::string off;
};

namespace boost { namespace serialization {

        template<class Archive>
            void serialize(Archive & ar, Block& b, const unsigned int version)
            {
                ar & b.numbers;
                ar & b.bits;
                ar & b.states;
                ar & b.off;
            }

} }

void save_Block(const Block &b, const char * filename){
    // make an archive
    std::ofstream ofs(filename);
    boost::archive::text_oarchive oa(ofs);
    oa << b;
}

int main()
{
    save_Block(Block { 
                   { 1, -42 },
                   { { 3, '0' }, { 17, '1' } },
                   { { "disabled", "true" }, { "locked", "false" }, { "ui", "manual" } },
                   "something called 'off'"
                },
                "test.txt");
}

The file test.txt will contain:

22 serialization::archive 10 0 0 2 0 1 -42 0 0 2 0 0 0 3 48 17 49 0 0 3 0 0 0 8 disabled 4 true 6 locked 5 false 2 ui 6 manual 22 something called 'off'

Leave a Reply

Your email address will not be published. Required fields are marked *