Reputation: 47
I have create a addressbook.proto with this i m good to generate below two files
addressbook.pb.h
addressbook.pb.cc
with protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/addressbook.proto
i have a code which read my address book name readproto.cc
readproto.cc
int main(int argc, char* argv[]) {
if (argc != 2) {
cerr << "Usage: " << argv[0] << " ADDRESS_BOOK_FILE" << endl;
return -1;
}
tutorial::AddressBook address_book;
{
// 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;
}
}
----
}
and i compile as
c++ readproto.cc addressbook.pb.cc `pkg-config --cflags --libs protobuf
i get executable file nothing bad but my doubt is what file should i load with this executable?
i tired as
./a.out addressbook.proto
Not sure which file need to load addressbook.proto is good ??
result : Failed to parse address book
.
i am new with protobuffer need help on it .Struggling from last three days this my last hope plss help with in this thank you
Upvotes: 0
Views: 558
Reputation: 373
Since you are using the protobuf tutorial, Call your program like this:
./a.out my_adress_book.bin
It will create an empty my_adress_book.bin and then prompt you to add entries.
This part creates an empty file:
fstream input(argv[1], ios::in | ios::binary);
if (!input) {
cout << argv[1] << ": File not found. Creating a new file." << endl;
}
....
Upvotes: 3
Reputation: 426
You have to specify the actual binary data. The data format is defined in the .proto file. Try to export the data to the file first. And then read it.
Upvotes: 0