gpt4 book ai didi

c++ - 在一个 Protocol Buffer 二进制文件中存储多条消息

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:52:55 24 4
gpt4 key购买 nike

我有重复的消息,我想将它们存储在一个文件中。目前我必须将这条重复的消息包装在另一条消息中。有解决办法吗?

package foo;

message Box {
required int32 tl_x = 1;
required int32 tl_y = 2;
required int32 w = 3;
required int32 h = 4;
}

message Boxes {
repeated Box boxes = 1;
}

最佳答案

这是"Techniques" Protocol Buffers 文档的一部分提到了重复消息:

If you want to write multiple messages to a single file or stream, it is up to you to keep track of where one message ends and the next begins. The Protocol Buffer wire format is not self-delimiting, so protocol buffer parsers cannot determine where a message ends on their own. The easiest way to solve this problem is to write the size of each message before you write the message itself. When you read the messages back in, you read the size, then read the bytes into a separate buffer, then parse from that buffer. (If you want to avoid copying bytes to a separate buffer, check out the CodedInputStream class (in both C++ and Java) which can be told to limit reads to a certain number of bytes.)

在 C++ 和 Java 中还有一种传统的实现方法。查看此 Stack Overflow 线程了解详细信息:Are there C++ equivalents for the Protocol Buffers delimited I/O functions in Java?

关于c++ - 在一个 Protocol Buffer 二进制文件中存储多条消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5586323/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com