我对二进制文件 ostringstream
有疑问。我想序列化谷歌的dense_hash_map。这可以使用文件句柄,但不能使用ostringstream
,即使文档声称这必须是可能的。
以下代码有效:
char *serializeTable( size_t &length ) {
// serialize to a temp file
FILE *f = fopen("D:\\Dumps\\Serialization2File.txt", "w");
bool result1 = serialize<Serializer, FILE>(m_serializer, f);
std::cout << "result1 = " << result1 << std::endl;
fseek(f, 0, SEEK_END);
int mylen = ftell(f);
fclose(f);
// read binary data from file
char *readbuf = new char[mylen];
std::ifstream rf("D:\\Dumps\\Serialization2File.txt", std::ios_base::binary);
rf.read(readbuf, mylen);
rf.close();
std::ofstream check("D:\\Dumps\\CheckSerializer.txt", std::ios_base::binary);
check.write(readbuf, mylen);
check.close();
length = mylen;
return readbuf;
}
以下代码仅打印出前 4 个符号。数组的其余部分由'\0'
s 组成:
char *serializeTable( size_t &length ) {
std::ostringstream output("", std::stringstream::out | std::stringstream::binary);
bool result = serialize<Serializer, std::ostringstream>(m_serializer, &output);
auto str = output.str();
std::cout << "str = " << str << std::endl;
}
输出:
str = W�B
代替:
E1eGQgAAAAAAAgAAAAAAAAAAksFLAAAAAAAAAAAAAAAAAGWwQAUAAAAAAAAAAAAAAAAAakANCg.....