Protobuf's demo program is
The C + + version of Protubuf has several serialize and Unserialize methods:
Method One:
The official demo program uses the
Copy Code code as follows:
Write The new Address book back to disk.
FStream output (argv[1], ios::out | ios::trunc | ios::binary);
if (!address_book. Serializetoostream (&output)) {
Cerr << "Failed to write Address Book." << Endl;
return-1;
}
Read the existing address book.
FStream input (argv[1], ios::in | ios::binary);
if (!input) {
cout << argv[1] <<: File not found. Creating a new file. "<< Endl;
else if (!address_book. Parsefromistream (&input)) {
Cerr << "Failed to parse Address Book." << Endl;
return-1;
}
The above is fstream, the data sequence (reverse sequence) in the disk file.
And if you want to sequence to char * and transfer through the socket, you can use:
Method Two:
Copy Code code as follows:
int size = Address_book. ByteSize ();
void *buffer = malloc (size);
Address_book. Serializetoarray (buffer, size);
Method Three:
Copy Code code as follows:
Using Ostringstream,
Std::ostringstream stream;
Address_book. Serializetoostream (&stream);
string text = Stream.str ();
char* Ctext = String.c_str ();