On Sun, Feb 16, 2014 at 11:09 AM, Jay Wilson <jawro...@gmail.com> wrote:
> I created some code that puts 10 GPB messages into a file. When I look at
> the file with a hex editor all of the messages are in the file.
>
> I now want to read the messages back in, processing them 1 at a time. The
> following produces only the last record in the input file. What am I
> missing? How do I read each message individually?

I think you forgot to create a record format. A common one is <varint>
<data> (where the varint says how many bytes the next record is).

>
>
> Yes I know the below is an infinite loop.
>
> require 'protobuf/message/message'
> require 'protobuf/message/enum'
> require 'protobuf/message/service'
> require 'protobuf/message/extend'
>
> module Test
>   class Interface < ::Protobuf::Message
>     defined_in __FILE__
>     required :string, :name, 1
>     optional :bool, :deleted, 2
>   end
>   class System < ::Protobuf::Message
>     defined_in __FILE__
>     required :string, :name, 1
>     optional :bool, :deleted, 2
>   end
>   class Rec < ::Protobuf::Message
>     defined_in __FILE__
>     optional :uint32, :timestamp, 1
>     optional :System, :system, 2
>     repeated :Interface, :interface, 3
>   end
> end
>
>
> jrec = Test::Rec.new
> counter = 1
> file = File.open("test", "r")
> while (jrec.parse_from_file(file))
>    puts "#{jrec.system.name}: #{counter}"
>    counter = counter + 1
> end
> file.close
>
>
> if I do a jrec.display, the last message in the file is what I see.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to protobuf+unsubscr...@googlegroups.com.
> To post to this group, send email to protobuf@googlegroups.com.
> Visit this group at http://groups.google.com/group/protobuf.
> For more options, visit https://groups.google.com/groups/opt_out.

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to