I have this simple .proto file:
package test;
message Person {
required string name = 1;
required int32 age = 2;
}
I have this test file to write contents:
#include <iostream>
#include <fstream>
#include <string>
#include "test.pb.h"
#include <stdio.h>
using namespace std;
int main(){
string data,dummy;
test::Person kavi;
kavi.set_name("Kavi");
kavi.set_age(22);
ofstream out;
out.open("data.dat");
kavi.SerializeToString(&data);
out<<data<<endl;
out.close();
ifstream in;
in.open("data.dat");
in>>dummy;
cout<<dummy<<endl;
cout<<data<<endl;
test::Person second;
second.ParseFromString(data);
cout<<
second.name();
cout<<"\n";
cout<<second.age();
in.close();
return 0;
}
When I tried to compile this, it works fine. But when I change the
line second.ParseFromString(data) to second.ParseFromString(dummy), it
compiles fine and on executing, it throws the following error:
"libprotobuf ERROR google/protobuf/message_lite.cc:123] Can't parse
message of type "test.Person" because it is missing required fields:
name, age"
Basically the strings dummy and data are the same. So why does it
throw the error?? Somebody please suggest a fix.
Thanks in advance,
Kavi