c++ - 在文件 C++ 中写入和读取结构的 3d vector

标签 c++ file map vector struct

我想保存这个结构并稍后加载它:

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

除了使用多个for 循环,还有什么办法吗? 我使用此功能进行保存:

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();
}

这个用于加载:

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
}

但是没有用,因为它只保存了指针!我该怎么办?

最佳答案

您不是很清楚 relationTable 是什么。 如果您想将结构保存到文件中,更简单的方法是使用 boost::serialization 见:How do you serialize an object in C++?

你可以这样做:

#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;
}
}}

完整的示例程序:

#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");
}

文件 test.txt 将包含:

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'

关于c++ - 在文件 C++ 中写入和读取结构的 3d vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18002889/

相关文章:

c++ - 如何隐藏另一个包含的静态库的API?

php - linux 正确标志传递 gcc mcrypt.h 位置

C 程序无法读取 .txt 文件中的用户名和密码

java - myfile.txt 中不存在任何内容

python - 从 python 中的文件中删除行的有效方法(并保持相同的文件名)?

printing - Mapfish 或 Geoserver 打印模块错误

Java 程序在通过 JNI 从 C++ 调用后提前终止而没有错误消息

Java LinkedHashMap 替换键

map - C++ wcout std::map 值

构造函数中的 C++ 保护声明