【问题标题】:Check an archive whether it is a binary/text/xml检查档案是否是二进制/文本/xml
【发布时间】:2021-04-05 05:37:51
【问题描述】:

我创建了一个输出 text_archive 并使用二进制存档恢复了它,显然,遇到了一些问题。 我能以某种方式知道存档的类型,以便我可以将适当的代码用于二进制/xml/文本存档。

class Info
{
private:
  // Allow serialization to access non-public data members.
  friend class boost::serialization::access;

  // Serialize the std::vector member of Info
  template<class Archive>
  void serialize(Archive & ar, const unsigned int version)
  {
    ar & filenames;
  }

  std::vector<std::string> filenames;


};


int main(int argc, char** argv)
{
  Info info;
  
   // Save filename data contained in Info object
  {
    // Create an output archive
    std::ofstream ofs( "store.dat" );
    **boost::archive::text_oarchive ar(ofs);**
    ar & info;
  }

  // Restore from saved data and print to verify contents
  Info restored_info;
  {
    // Create and input archive
    std::ifstream ifs( "store.dat" );
    **boost::archive::binary_iarchive ar(ifs);**
    // Load the data
    ar & restored_info;
  }
  return 0;
}

【问题讨论】:

    标签: c++ boost archive boost-serialization


    【解决方案1】:

    您可以将前两个字节用于特定类型,比如说

    00 表示二进制 01 为 xml 10 用于文本

    内容的重置是为了数据本身

    【讨论】:

      猜你喜欢
      • 2011-02-11
      • 2022-01-13
      • 1970-01-01
      • 2011-03-06
      • 2018-07-15
      • 1970-01-01
      • 2010-10-25
      • 2018-05-09
      • 1970-01-01
      相关资源
      最近更新 更多