【发布时间】:2015-06-13 11:44:25
【问题描述】:
我正在尝试使用Google Protocol Buffers 从文件中读取多条消息。使用CodedInputStream 的文档suggests。
但如果我尝试阅读的不仅仅是一条非常小的消息,我会收到来自MergeFromCodedStream 的失败
例如,如果我有一条消息定义为:
message Chunk {
repeated int64 values = 1 [packed=true];
}
并尝试将消息写入文件,然后将其读回:
int main() {
GOOGLE_PROTOBUF_VERIFY_VERSION;
{
Chunk chunk;
for (int i = 0; i != 26; ++i)
chunk.add_values(i);
std::ofstream output("D:\\temp.bin");
OstreamOutputStream raw_output(&output);
if (!writeDelimitedTo(chunk, &raw_output)){
std::cout << "Unable to write chunk\n";
return 1;
}
}
{
std::ifstream input("D:\\temp.bin");
IstreamInputStream raw_input(&input);
Chunk in_chunk;
if (!readDelimitedFrom(&raw_input, &in_chunk)) { // <--- Fails here
std::cout << "Unable to read chunk\n";
return 1;
}
std::cout << "Num values in chunk " << in_chunk.values_size() << "\n";
}
google::protobuf::ShutdownProtobufLibrary();
}
其中writeDelimitedTo 和readDelimitedFrom 来自C++ protobuf 库的作者this answer:
bool writeDelimitedTo(
const google::protobuf::MessageLite& message,
google::protobuf::io::ZeroCopyOutputStream* rawOutput) {
google::protobuf::io::CodedOutputStream output(rawOutput);
const int size = message.ByteSize();
output.WriteVarint32(size);
uint8_t* buffer = output.GetDirectBufferForNBytesAndAdvance(size);
if (buffer != NULL) {
message.SerializeWithCachedSizesToArray(buffer);
} else {
message.SerializeWithCachedSizes(&output);
if (output.HadError()) return false;
}
return true;
}
bool readDelimitedFrom(
google::protobuf::io::ZeroCopyInputStream* rawInput,
google::protobuf::MessageLite* message) {
google::protobuf::io::CodedInputStream input(rawInput);
uint32_t size;
if (!input.ReadVarint32(&size)) return false;
google::protobuf::io::CodedInputStream::Limit limit =
input.PushLimit(size);
if (!message->MergeFromCodedStream(&input)) return false; // <-- Fails here
if (!input.ConsumedEntireMessage()) return false;
input.PopLimit(limit);
return true;
}
如果我只在我的消息中写入 25 个值,它可以工作,26 个它会失败。我已经在代码中显示了失败的地方。
我尝试调试 protobuf 库,但似乎无法将新数据读入缓冲区,但我不知道为什么。
我正在使用 Visual Studio 2013 和 protobuf 2.6.1。
【问题讨论】:
-
您是说您更改了
main()顶部的循环,以便将限制从i != 26;更改为其他内容,例如i != 30;,并且您无法读取30 个值你加了?MergeFromCodedStream()函数是否产生任何错误代码?如果您在消息中写入较少数量的值,例如 22,会发生什么情况?顺便说一句,我建议你修改你的两个函数writeDelimitedTo()和readDelimitedFrom()以返回一个特定的错误代码,该代码将指示函数在什么时候失败,而不是仅仅返回一个bool。 -
@RichardChambers 基本上,是的。如果我将循环更改为
i != 25,它将按预期工作。如所写,i != 26失败。据我所知,MergeFromCodedStream没有返回错误代码。只是一个表示成功或失败的布尔值。 -
您可能需要在
std::ofstream和std::ifstream构造函数上添加std::ios::binary行结束标志。 -
@rhashimoto 啊,当然!谢谢!
-
这个使用 C++ 的例子展示了在创建和写入输出文件时使用
std::ios::trunc和std::ios::binary。 developers.google.com/protocol-buffers/docs/cpptutorial
标签: c++ protocol-buffers