我使用 protobuf-net 序列化数据,并且能够在 C# 中进行相同的处理。
放置一个 C# 虚拟 w#
var file = File.Create("animal.bin");
//Creating Msg - Fill the Data
animal.id = "1";
animal.Name = "Rat";
animal.host = "Cheetha";
ProtoBuf.Serializer.SerializeWithLengthPrefix(file, animal, PrefixStyle.Base128, 1);
animal.id = "2";
animal.Name = "Cat";
animal.host = "Cheetha";
ProtoBuf.Serializer.SerializeWithLengthPrefix(file, animal, PrefixStyle.Base128, 1);
....
animal.id = "4";
animal.name = "Cheetha";
animal.host = "Cheetha";
ProtoBuf.Serializer.SerializeWithLengthPrefix(file, animal, PrefixStyle.Base128, 1);
//Done Creating Msg
file.Close();
到目前为止一切顺利......这里没有问题。但是当我尝试使用协议缓冲区在 C++ 中反序列化相同的内容时,我无法获得正确的数据
cpp代码...
GOOGLE_PROTOBUF_VERIFY_VERSION;
string fpath = "animal.bin";
fstream input(fpath, ios::in | ios::binary);
if (!input)
{
cerr << "failed to open " << fpath << endl;
return false;
}
ZeroCopyInputStream *raw_in = new IstreamInputStream(&input);
CodedInputStream *coded_in = new CodedInputStream(raw_in);
google::protobuf::uint32 n;
std::string tmpStr;
animal::animalInfo animalList;
coded_in->ReadVarint32(&n);
cout << "# " << n << endl; //output: #10
coded_in->ReadRaw(&tmpStr,n); //tmpStr shows data like >>1..Rat..Ch
animalList.ParseFromArray(&tmpStr,1);//Not sure if this is correct usage?
我确定我犯了一个错误,但无法理解什么是错的......已经阅读并重读了很多关于此的帖子,但看不出还有什么问题
使用 Protocol Buffer2.5、protobuf-netR622、Visual Studio 2010