Analysis of implementation method based on Protobuf C + + serialize to char* _c language

Source: Internet
Author: User

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 ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.