好吧,我想通了。将文本 proto 文件读入对象....
#include <iostream>
#include <fcntl.h>
#include <fstream>
#include <google/protobuf/text_format.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include "YourProtoFile.pb.h"
using namespace std;
int main(int argc, char* argv[])
{
// Verify that the version of the library that we linked against is
// compatible with the version of the headers we compiled against.
GOOGLE_PROTOBUF_VERIFY_VERSION;
Tasking *tasking = new Tasking(); //My protobuf object
bool retValue = false;
int fileDescriptor = open(argv[1], O_RDONLY);
if( fileDescriptor < 0 )
{
std::cerr << " Error opening the file " << std::endl;
return false;
}
google::protobuf::io::FileInputStream fileInput(fileDescriptor);
fileInput.SetCloseOnDelete( true );
if (!google::protobuf::TextFormat::Parse(&fileInput, tasking))
{
cerr << std::endl << "Failed to parse file!" << endl;
return -1;
}
else
{
retValue = true;
cerr << "Read Input File - " << argv[1] << endl;
}
cerr << "Id -" << tasking->taskid() << endl;
}
当我在终端执行它时,我的程序将 proto buff 的输入文件作为第一个参数。例如./myProg inputFile.txt
希望这可以帮助任何有同样问题的人