1 // See README.txt for information and build instructions.
6 #include "addressbook.pb.h"
9 // Iterates though all people in the AddressBook and prints info about them.
10 void ListPeople(const tutorial::AddressBook& address_book) {
11 for (int i = 0; i < address_book.person_size(); i++) {
12 const tutorial::Person& person = address_book.person(i);
14 cout << "Person ID: " << person.id() << endl;
15 cout << " Name: " << person.name() << endl;
16 if (person.has_email()) {
17 cout << " E-mail address: " << person.email() << endl;
20 for (int j = 0; j < person.phone_size(); j++) {
21 const tutorial::Person::PhoneNumber& phone_number = person.phone(j);
23 switch (phone_number.type()) {
24 case tutorial::Person::MOBILE:
25 cout << " Mobile phone #: ";
27 case tutorial::Person::HOME:
28 cout << " Home phone #: ";
30 case tutorial::Person::WORK:
31 cout << " Work phone #: ";
34 cout << phone_number.number() << endl;
39 // Main function: Reads the entire address book from a file and prints all
40 // the information inside.
41 int main(int argc, char* argv[]) {
42 // Verify that the version of the library that we linked against is
43 // compatible with the version of the headers we compiled against.
44 GOOGLE_PROTOBUF_VERIFY_VERSION;
47 cerr << "Usage: " << argv[0] << " ADDRESS_BOOK_FILE" << endl;
51 tutorial::AddressBook address_book;
54 // Read the existing address book.
55 fstream input(argv[1], ios::in | ios::binary);
56 if (!address_book.ParseFromIstream(&input)) {
57 cerr << "Failed to parse address book." << endl;
62 ListPeople(address_book);
64 // Optional: Delete all global objects allocated by libprotobuf.
65 google::protobuf::ShutdownProtobufLibrary();