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. }  
  • 相关阅读:
    115.子集和的目标值(大数据的01背包)
    116. 张程易,编程易(01背包)
    110.科技庄园(多重背包)(未结题)
    113.失恋28天-缝补礼物(多重背包)
    109.关路灯(区间dp)
    107.01背包变式题型:传纸条
    cojs.tk(所有题目来源) 树状数组专练
    在线评测的网站
    108.方格取数
    106.运输装备(二维01背包)
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13318630.html
Copyright © 2011-2022 走看看