首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将结构写入.txt文件

问将结构写入.txt文件
EN

Stack Overflow用户
提问于 2021-08-23 08:26:55
回答 1查看 194关注 0票数 2

我试图使用boost将我的struct存储到txt文件中,但是无法做到。我在用boost库。示例结构

代码语言:javascript
复制
struct Frame
{
    uint32_t address{ 0 };
    uint16_t marks{ 0 };
    uint16_t age{ 0 };
    char gender{ 'M' };
    std::string userName;
};

对于二进制,有简单的代码

代码语言:javascript
复制
boost::archive::binary_oarchive ar(ofs, boost::archive::no_header);
ar << boost::serialization::make_binary_object(&f, sizeof(Frame));

假设文件是用fstream对象打开的,而“f”是“框架”的对象。

我想知道是否有类似的方式将结构写入txt文件,并且我不想一个接一个地编写数据类型。假设我们不知道结构中数据类型的类型/数量。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-23 13:50:12

正如其他人所评论的,您将不得不提供序列化帮助程序,告诉Boost如何按成员顺序序列化。

如果只有这样的聚合,则可以使用Boost PFR在一定程度上自动生成此函数。

代码语言:javascript
复制
pfr::for_each_field(s, [&](auto&& f) { ar & f; });               

下面是一个例子:

代码语言:javascript
复制
namespace MyLib {
    struct Frame {
        uint32_t    address{0};
        uint16_t    marks{0};
        uint16_t    age{0};
        char        gender{'M'};
        std::string userName;
    };

    struct Other {
        std::string userName;
        std::map<uint32_t, std::string> properties;
    };

} // namespace MyLib

注意,为了获得良好的样式,我将它们放置在名称空间中,这样我们就可以突出显示ADL用于查找serialize重载。现在,让我们定义重载:

代码语言:javascript
复制
namespace MyLib {
    #define SERIALIZER(Aggregate)                                                  \
        template <typename Archive>                                                \
        void serialize(Archive& ar, Aggregate& s, unsigned version)                \
        {                                                                          \
            pfr::for_each_field(s, [&](auto&& f) { ar & f; });               \
        }


    SERIALIZER(Frame)
    SERIALIZER(Other)
} // namespace MyLib

使用宏我们可以避免重复的代码。当然,您也可以在没有宏的情况下做到这一点。

现在您可以将两者序列化。假设我们有:

代码语言:javascript
复制
MyLib::Frame const diablo{178, 42, 37, 'F', "Diablo"};
MyLib::Other const other{"diablo", {{1, "one"}, {2, "two"}, {3, "three"},}};

然后序列化到文本流:

代码语言:javascript
复制
boost::archive::text_oarchive oa(ss);
oa & diablo;
oa & other;

已经导致了流,包括。

代码语言:javascript
复制
22 serialization::archive 19 0 0 178 42 37 70 6 Diablo 0 0 6 diablo 0 0 3 0 0
 0 1 3 one 2 3 two 3 5 three

全演示

此演示程序检查反序列化的结果是否实际上与原始结构相同:

住在Coliru

代码语言:javascript
复制
#include <string>
#include <map>

namespace MyLib {
    struct Frame {
        uint32_t    address{0};
        uint16_t    marks{0};
        uint16_t    age{0};
        char        gender{'M'};
        std::string userName;
    };

    struct Other {
        std::string userName;
        std::map<uint32_t, std::string> properties;
    };

} // namespace MyLib

#include <boost/pfr.hpp>
namespace pfr = boost::pfr;

namespace MyLib {
#define SERIALIZER(Aggregate)                                                  \
    template <typename Archive>                                                \
    void serialize(Archive& ar, Aggregate& s, unsigned version)                \
    {                                                                          \
        pfr::for_each_field(s, [&](auto&& f) { ar & f; });               \
    }

    SERIALIZER(Frame)
    SERIALIZER(Other)
} // namespace MyLib

#include <iostream>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/map.hpp>

int main() {
    MyLib::Frame const diablo{178, 42, 37, 'F', "Diablo"};
    MyLib::Other const other{"diablo", {{1, "one"}, {2, "two"}, {3, "three"},}};

    std::stringstream ss;
    {
        boost::archive::text_oarchive oa(ss);
        oa << diablo;
        oa << other;
    }

    std::cout << ss.str() << "\n";

    {
        boost::archive::text_iarchive ia(ss);
        MyLib::Frame f;
        MyLib::Other o;

        ia >> f >> o;

        std::cout << std::boolalpha;
        std::cout << "Frame identical: " << pfr::eq(diablo, f) << "\n";
        std::cout << "Other identical: " << pfr::eq(other, o) << "\n";
    }
}

打印

代码语言:javascript
复制
g++ -std=c++2a -O2 -Wall -pedantic -pthread main.cpp -lboost_serialization && ./a.out
22 serialization::archive 19 0 0 178 42 37 70 6 Diablo 0 0 6 diablo 0 0 3 0 0 0 1 3 one 2 3 two 3 5 three

Frame identical: true
Other identical: true
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68889432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档