Skip to content

Instantly share code, notes, and snippets.

@blogdarkspot
Created November 6, 2016 23:58
Show Gist options
  • Save blogdarkspot/c16e63303875dae19207601546196db6 to your computer and use it in GitHub Desktop.
Save blogdarkspot/c16e63303875dae19207601546196db6 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <string>
#include "addressbook.pb.h"
using namespace std;
void ListPeople( const protobuf_example::AddressBook& _address_book)
{
for (int i =0; i < _address_book.person_size(); i++)
{
const protobuf_example::Person& person = _address_book.person(i);
cout << "Person ID" << person.id() << endl;
cout << "Name " << person.name() << endl;
cout << " E-mail address: " << person.email() << endl;
for (int j = 0; j < person.phone_size(); j++)
{
const protobuf_example::Person::PhoneNumber& phone = person.phone(j);
switch(phone.type())
{
case protobuf_example::Person::MOBILE:
cout << " Mobile phone #: "; break;
case protobuf_example::Person::HOME:
cout << " Home phone #: "; break;
case protobuf_example::Person::WORK:
cout << " Work phone #: "; break;
}
cout << phone.number() << endl;
}
}
}
int main(int argc, char *argv[])
{
GOOGLE_PROTOBUF_VERIFY_VERSION;
if (argc != 2)
{
cerr << "Usage: " << argv[0] << " ADDRESS_BOOK_FILE" << endl;
return -1;
}
protobuf_example::AddressBook address_book;
{
fstream input(argv[1], ios::in | ios::binary);
if (!address_book.ParseFromIstream(&input))
{
cerr << "Failed to parse address book. " << endl;
return -1;
}
}
ListPeople(address_book);
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment