Created
November 6, 2016 23:57
-
-
Save blogdarkspot/f2a3e51d79690d3f60c61e983875855d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include "addressbook.pb.h" | |
using namespace std; | |
void PromptForAddress(protobuf_example::Person * person) | |
{ | |
cout << "Enter person ID number: "; | |
int id; | |
cin >> id; | |
person->set_id(id); | |
cin.ignore(256, '\n'); | |
cout << "Enter name: "; | |
getline(cin, *person->mutable_name()); | |
cout << "Enter email address (blank for none): "; | |
string email; | |
getline(cin, email); | |
if (!email.empty()) person->set_email(email); | |
while(true) | |
{ | |
cout << "Enter a phone number (or leave blank to finish): "; | |
string number; | |
getline(cin, number); | |
if(number.empty()) break; | |
protobuf_example::Person::PhoneNumber* phone_number = person->add_phone(); | |
phone_number->set_number(number); | |
cout << "Is this a mobile, home or work phone? "; | |
string type; | |
getline(cin, type); | |
if (type == "mobile") | |
{ | |
phone_number->set_type(protobuf_example::Person::MOBILE); | |
} | |
else if (type == "home") | |
{ | |
phone_number->set_type(protobuf_example::Person::HOME); | |
} | |
else if (type == "work") | |
{ | |
phone_number->set_type(protobuf_example::Person::WORK); | |
} | |
else | |
{ | |
cout << "Unkown phone type" << 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(!input) | |
{ | |
cout << argv[1] << " : file not found" << endl; | |
} else if (!address_book.ParseFromIstream(&input)) | |
{ | |
cerr << "Failed to parse address book. " << endl; | |
return -1; | |
} | |
} | |
PromptForAddress(address_book.add_person()); | |
{ | |
fstream output(argv[1], ios::out | ios::trunc | ios::binary); | |
if (!address_book.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