Protobuf messages are not aware of their own sizes, and you haven't
told the input stream how many bytes of message it should consume. Try
this:

coded_input->ReadLittleEndian32(&objtype);
coded_input->ReadLittleEndian32(&objlen);
CodedInputStream::Limit lim = coded_input->PushLimit(objlen);
tMsg.ParseFromCodedStream(coded_input);
coded_input->PopLimit(lim);

(Note, untested, just I think this is your problem).

Chris

On Thu, 8 Dec 2011 22:48:25 -0800 (PST)
alok <alok.jad...@gmail.com> wrote:

> I created a binary file using a c++ program using protocol buffers. I
> had issues reading the binary file in my C# program, so I decided to
> write a small c++ program to test the reading.
> 
> My proto file is as follows
> 
> message TradeMessage {
> required double timestamp = 1;
> required string ric_code = 2;
> required double price = 3;
> required int64 size = 4;
> required int64 AccumulatedVolume = 5;
> }
> 
> When writing to protocol buffer, I first write the object type, then
> object length and the object itself.
> 
> coded_output->WriteLittleEndian32((int) ObjectType_Trade);
> coded_output->WriteLittleEndian32(trade.ByteSize());
> trade.SerializeToCodedStream(coded_output);
> 
> Now, when I am trying to read the same file in my c++ program i see
> strange behavior.
> 
> My reading code is as follows:
> 
> coded_input->ReadLittleEndian32(&objtype);
> coded_input->ReadLittleEndian32(&objlen);
> tMsg.ParseFromCodedStream(coded_input);
> cout << "Expected Size = " << objlen << endl;
> cout<<" Trade message received for: "<< tMsg.ric_code() << endl;
> cout << "TradeMessage Size = " << tMsg.ByteSize() << endl;
> 
> In this case, i get following output
> 
> Expected Size = 33
> Trade message received for: .CSAP0104
> TradeMessage Size = 42
> 
> When I write to file, I write trade.ByteSize() as 33 bytes, but when I
> read the same object, the object ByteSize() is 42 bytes i.e. it is
> trying to read 42 bytes. But it should be trying to read 33 bytes.
> This affects the rest of the fiel reading. I am not sure what is wrong
> in this. Please advice.
> 
> just to double check.. I compared the protocol buffer generated files
> in my reader and writer projects. The generated files are identical.
> So I guess, the file coding is different for some reason. I do not
> understand why it is different.
> 
> 
> Regards,
> Alok
> 
> Regards, Alok
> 

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to protobuf@googlegroups.com.
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.

Reply via email to