简体   繁体   English

检查档案是否是二进制/文本/xml

[英]Check an archive whether it is a binary/text/xml

I created an output text_archive and I restored it using binary archive and obviously, got some issues.我创建了一个 output text_archive 并使用二进制存档恢复了它,显然,遇到了一些问题。 Can I know somehow the kind of archive, so that I could possibly use the appropriate code for binary/xml/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;
}

You can use first two bytes to specific type, let's say您可以将前两个字节用于特定类型,比如说

00 for binary 01 for xml 10 for text 00 表示二进制 01 表示 xml 10 表示文本

and the reset of the content is for data itself内容的重置是针对数据本身的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM