serialize to a file using FileOutputStream

490 views
Skip to first unread message

Paul Yang

unread,
Oct 11, 2010, 4:07:04 PM10/11/10
to Protocol Buffers
Hi,
I am new to protocol buffers, and I am trying to serialize a message
to a file. I need to serialize the file so that in can be opened in
Java using parseDelimitedFrom(). However, when I write to the file
using SerializeToCodedStream or SerializeWithCachedSizes, nothing
happens to the serializedMessage.bin file, and it stays at 0 bytes.
Why is nothing being written to the file? I have attached the code
below:

int fd = open("serializedMessage.bin", O_WRONLY);

google::protobuf::io::ZeroCopyOutputStream* fileOutput = new
google::protobuf::io::FileOutputStream(fd);

google::protobuf::io::CodedOutputStream* codedOutput = new
google::protobuf::io::CodedOutputStream(fileOutput);

codedOutput->WriteVarint32(message1.ByteSize());
message1.SerializeToCodedStream(codedOutput);
// also tried snap1.SerializeWithCachedSizes(codedOutput)

close(fd);

Thanks!

Daniel Wright

unread,
Oct 11, 2010, 7:03:04 PM10/11/10
to Paul Yang, Protocol Buffers
I think it's being buffered in the FileOutputStream -- you should be sure to delete the output streams (in the reverse order that you created them) before you close the file.


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


Paul Yang

unread,
Oct 11, 2010, 8:06:42 PM10/11/10
to Protocol Buffers
thanks! I tried that and it works now!

On Oct 11, 4:03 pm, Daniel Wright <dwri...@google.com> wrote:
> I think it's being buffered in the FileOutputStream -- you should be sure to
> delete the output streams (in the reverse order that you created them)
> before you close the file.
>
> On Mon, Oct 11, 2010 at 1:07 PM, Paul Yang <mjpabl...@gmail.com> wrote:
> > Hi,
> > I am new to protocol buffers, and I am trying to serialize a message
> > to a file.  I need to serialize the file so that in can be opened in
> > Java using parseDelimitedFrom().  However, when I write to the file
> > using SerializeToCodedStream or SerializeWithCachedSizes, nothing
> > happens to the serializedMessage.bin file, and it stays at 0 bytes.
> > Why is nothing being written to the file?  I have attached the code
> > below:
>
> > int fd = open("serializedMessage.bin", O_WRONLY);
>
> > google::protobuf::io::ZeroCopyOutputStream* fileOutput = new
> > google::protobuf::io::FileOutputStream(fd);
>
> > google::protobuf::io::CodedOutputStream* codedOutput = new
> > google::protobuf::io::CodedOutputStream(fileOutput);
>
> > codedOutput->WriteVarint32(message1.ByteSize());
> > message1.SerializeToCodedStream(codedOutput);
> > // also tried snap1.SerializeWithCachedSizes(codedOutput)
>
> > close(fd);
>
> > Thanks!
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Protocol Buffers" group.
> > To post to this group, send email to prot...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > protobuf+u...@googlegroups.com<protobuf%2Bunsu...@googlegroups.com>
> > .
Reply all
Reply to author
Forward
0 new messages