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. }  
  • 相关阅读:
    支付宝H5 与网页端支付开发
    java图片操作--生成与原图对称的图片
    java 图片的自定义大小
    微信公众号开发(2)---消息的接收发送
    js 创建对象
    jqery多选
    金额大写转换
    js数字转换
    js日期格式转换
    java设计模式
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13318630.html
Copyright © 2011-2022 走看看