简体   繁体   English

将二进制ostringstream转换为google :: dense_hash_map中的char数组

[英]Convert binary ostringstream to char array in google::dense_hash_map

I have a problem with a binary ostringstream . 我对二进制 ostringstream有问题。 I want to serialize Google's dense_hash_map. 我想序列化Google的density_hash_map。 This is possible using a file handle, but not using a ostringstream , even though the docs claim this must be possible . 使用文件句柄是可行的,但不能使用ostringstream ,即使文档声称这是必须的

The following code works: 以下代码有效:

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;
}

The following code prints out only the first 4 symbols. 以下代码仅打印出前4个符号。 The rest of the array consists of '\\0' s: 数组的其余部分由'\\0'组成:

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;
}

Output: 输出:

str = W�B

instead of: 代替:

E1eGQgAAAAAAAgAAAAAAAAAAksFLAAAAAAAAAAAAAAAAAGWwQAUAAAAAAAAAAAAAAAAAakANCg.....

After some time searching in docs and trying around, I figured out the answer myself. 经过一段时间的文档搜索并尝试后,我自己找出了答案。

For accessing separate characters stored in the stream, one should use a stream iterator ( http://www.dyn-lab.com/articles/c-streams.html , part 6). 为了访问流中存储的单独字符,应该使用流迭代器( http://www.dyn-lab.com/articles/c-streams.html ,第6部分)。

My code now looks the following way: 现在,我的代码如下所示:

char *serializeTable( size_t &length ) {

    std::stringstream stream("", std::stringstream::out | std::stringstream::in | std::stringstream::binary);
    //std::ostringstream output("", std::stringstream::binary);


    bool result = serialize<Serializer, std::stringstream>(m_serializer, &stream);
    std::istreambuf_iterator<char> itt(stream.rdbuf()), eos;
    std::vector<char> serialVector;
    serialVector.reserve(619999); // just a guess
    while(itt != eos) {
        char c = *itt++;
        serialVector.push_back(c);
    }

    length = serialVector.size();

    char *serial = new char[length];
    int index = 0;
    for each(char a in serialVector) {
        serial[index++] = a;
    }
    return serial;
}

Thanks for the comments, everyone! 谢谢大家的评论!

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

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