zoukankan      html  css  js  c++  java
  • boost binary 序列化

    在用boost 二进制序列化类的时候,需要注意动态调用文件打开标志std::ios::binary: 


    The flag std::ios::binary is required only in Windows, where the default mode (ascii) would translate to (and vice versa), thus corrupting any data that is not textual. 

    C++代码  收藏代码
    1. #include <boost/serialization/string.hpp>  
    2. #include "boost/archive/binary_oarchive.hpp"  
    3. #include "boost/archive/binary_iarchive.hpp"  
    4. #include "boost/iostreams/stream.hpp"  
    5. #include <boost/serialization/version.hpp>  
    6.   
    7. typedef struct tagWorkStruct  
    8. {  
    9.     string groundTruthFile;  
    10.     string jpegFile;  
    11.     int workId;  
    12. }WorkStruct;  
    13.   
    14. template<class Archive>  
    15. void serialize(Archive &ar, AddNewCardWorkStruct &g, const unsigned int version)  
    16. {  
    17.     ar & g.groundTruthFile;  
    18.     ar & g.jpegFile;  
    19.     ar & g.workId;  
    20. }  
    21.   
    22.   
    23. //写入文件  
    24. string fileName = "test.dat";  
    25. ofstream os(fileName.c_str(), std::ios::binary);  
    26. if (!os.fail())  
    27. {  
    28.     try  
    29.     {  
    30.         WorkStruct tempStruct;  
    31.         boost::archive::binary_oarchive oa(os);  
    32.         oa << tempSturct;  
    33.     }  
    34.     catch(...)   
    35.     {  
    36.     }  
    37. }  
    38.   
    39.   
    40. //读取文件  
    41. ifstream is(dataFileName.c_str(), std::ios::binary);  
    42. if (!is.fail())  
    43. {  
    44.    try  
    45.    {  
    46.     WorkStruct tempStruct;  
    47.     boost::archive::binary_iarchive ia(is);  
    48.     ia >> tempStruct;  
    49.    }  
    50.    catch (...)//boost::archive::archive_exception  
    51.    {  
    52.    }  
    53. }  
  • 相关阅读:
    echart------属性详细介绍
    网页链接(插件,判断服务)
    简单的轮播效果
    实时时间
    Oracle Partition By 的使用
    Java配置----JDK开发环境搭建及环境变量配置
    流程控制语句以及引号的使用
    解决报表表头格式问题
    k3could报表中替换最后行汇总字段方法
    k3cloud中使用委托添加提示对话框点击确定后执行方法
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13318630.html
Copyright © 2011-2022 走看看