输出更多的东西比一个多态文本存档 [英] Outputting more things than a Polymorphic Text Archive
问题描述
我使用的是鲨鱼机器学习库,它输出的分类使用的boost ::存档:: polymorphic_text_(IO)存档
类文件。
I'm using the Shark machine learning library, and it outputs its classifiers to file by using the boost::archive::polymorphic_text_(io)archive
classes.
我创建的话模型,我还需要写入文件(用我自己的code)的包,我还需要输出到文件。
I'm creating a bag of words model that I also need to write to file (using my own code) and I need to also output that to file.
我将理想喜欢其输出到同一个文件的分类。是有可能当一个多态文本文档是用来写东西到同一个文件?难道是不够的,只是通过在的fstream归档开始的地方?
I would ideally like to output this to the same file as the classifier. Is is possible to write things to the same file as when a polymorphic text archive is used? Is it enough to just pass the fstream at the point the archive begins?
编辑:只是要稍微更清楚:是否提升支持我把其他的东西在一个文件中除了这些档案
Just to be slightly clearer: Does Boost support me putting other things in a file alongside these archives?
推荐答案
首先:的都未被档案
First Off: Streams Are Not Archives.
我的第一反应是你尝试过。但是,我很感兴趣,但没有找到这个文档中任何事情,所以我做了一些测试自己:
My first reaction would be "have you tried". But, I was intrigued and couldn't find anything about this in the documentation, so I did a few tests myself:
- 的答案似乎是否,它不支持
- 这似乎二进制档案工作
- 这似乎打破,因为XML /文本存档后走人
为0xA
输入缓冲区字符。如果下一个存档读取的文本以及这些不会构成问题,但显然打破二进制存档。
- the answer seems to be "No", it's not supported
- it seems to work for binary archives
- it seems to break down because the xml/text archives leave trailing
0xa
characters in the input buffer. These will not pose a problem if the "next" archive to be read is text as well, but obviously break binary archives.
下面是我的测试:
<大骨节病> 住在Coliru 骨节病>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
int data = 42;
template <typename Ar>
void some_output(std::ostream& os)
{
std::cout << "Writing archive at " << os.tellp() << "\n";
Ar ar(os);
ar << BOOST_SERIALIZATION_NVP(data);
}
template <typename Ar>
void roundtrip(std::istream& is)
{
data = -1;
std::cout << "Reading archive at " << is.tellg() << "\n";
Ar ar(is);
ar >> BOOST_SERIALIZATION_NVP(data);
assert(data == 42);
}
#include <sstream>
int main()
{
std::stringstream ss;
//some_output<boost::archive::text_oarchive>(ss); // this derails the binary archive that follows
some_output<boost::archive::binary_oarchive>(ss);
some_output<boost::archive::xml_oarchive>(ss);
some_output<boost::archive::text_oarchive>(ss);
//roundtrip<boost::archive::text_iarchive>(ss);
roundtrip<boost::archive::binary_iarchive>(ss);
roundtrip<boost::archive::xml_iarchive>(ss);
roundtrip<boost::archive::text_iarchive>(ss);
// just to prove that there's remaining whitespace
std::cout << "remaining: ";
char ch;
while (ss>>std::noskipws>>ch)
std::cout << " " << std::showbase << std::hex << ((int)(ch));
std::cout << "\n";
// of course, anything else will fail:
try {
roundtrip<boost::archive::text_iarchive>(ss);
} catch(boost::archive::archive_exception const& e)
{
std::cout << "Can't deserialize from a stream a EOF: " << e.what();
}
}
打印:
Writing archive at 0
Writing archive at 44
Writing archive at 242
Reading archive at 0
Reading archive at 44
Reading archive at 240
remaining: 0xa
Reading archive at 0xffffffffffffffff
Can't deserialize from a stream a EOF: input stream error
这篇关于输出更多的东西比一个多态文本存档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!