Skip to content

Instantly share code, notes, and snippets.

@laclefyoshi
Created July 16, 2011 04:44
Show Gist options
  • Save laclefyoshi/1086012 to your computer and use it in GitHub Desktop.
Save laclefyoshi/1086012 to your computer and use it in GitHub Desktop.
cpp code for Protocol Buffers
/**
Copyright: (c) SAEKI Yoshiyasu
License : MIT-style license
<http://www.opensource.org/licenses/mit-license.php>
last updated: 2011/07/16
**/
#include <iostream>
#include <fstream>
#include <string>
#include "person.pb.h"
using namespace std;
int main(int argc, char* argv[]) {
GOOGLE_PROTOBUF_VERIFY_VERSION;
fstream input("PersonBook", ios::in | ios::binary);
if (!input) {
cout << argv[1] << ": File not found. Creating a new file." << endl;
}
Person p1;
p1.set_id(1);
p1.set_name("saeki");
p1.set_email("saeki@example.org");
Person p2;
p2.set_id(2);
p2.set_name("furukawa");
p2.set_email("furukawa@example.org");
fstream output("PersonBook", ios::out | ios::trunc | ios::binary);
if (!p1.SerializeToOstream(&output)
|| !p2.SerializeToOstream(&output)) {
cerr << "Failed to write address book." << endl;
return -1;
}
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment