On the page
"https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.io.coded_stream"
there is an example at the top of the page which contains code that appears
to show a user how to create and serialize a message to a file. However the
flags to open the file lack the O_CREAT flag which allows creating the file
if it doesn't exist. I was troubleshooting a situation where this snippet
was used and compiled, but never created a file.
The full snippet is below along with my correction below that.
Hopefully this saves others some time after copy/pasting that example and
finding it doesn't write anything.
Cheers,
Zach Davis
int fd = open("myfile", O_WRONLY);
ZeroCopyOutputStream* raw_output = new FileOutputStream(fd);
CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
int magic_number = 1234;
char text[[]] = "Hello world!";
coded_output->WriteLittleEndian32(magic_number);
coded_output->WriteVarint32(strlen(text));
coded_output->WriteRaw(text, strlen(text));
delete coded_output;
delete raw_output;
close(fd);
int fd = open("myfile", O_CREAT | O_WRONLY);
ZeroCopyOutputStream* raw_output = new FileOutputStream(fd);
CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
int magic_number = 1234;
char text[[]] = "Hello world!";
coded_output->WriteLittleEndian32(magic_number);
coded_output->WriteVarint32(strlen(text));
coded_output->WriteRaw(text, strlen(text));
delete coded_output;
delete raw_output;
close(fd);
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.