Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse in text file for Google Protocol Buffer

According to the example code https://developers.google.com/protocol-buffers/docs/cpptutorial, they show how to parse in a proto file that is in binary format. using

tutorial::AddressBook address_book;

{
  // Read the existing address book.
  fstream input(argv[1], ios::in | ios::binary);
  if (!address_book.ParseFromIstream(&input)) {
    cerr << "Failed to parse address book." << endl;
    return -1;
  }
}

I tried removing the ios::binary for my input file that is in text format, but that still fails at reading in the file. What do I need to do to read in a proto file in text format?

like image 695
user1314238 Avatar asked May 31 '12 22:05

user1314238


People also ask

Is Protobuf text or binary?

Protobuf is a binary format, so reading and writing should be done as binary, not text. If you don't want binary format, you should consider using something other than protobuf (there are lots of textual data formats, such as XML, JSON, CSV); just using text abstractions is not enough.

Does Google use Protobuf?

Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler.

Does Protobuf handle endianness?

Protocol buffers messages always use little-endian encoding. Implementations running on big-endian architectures should be doing the conversions automatically. If you are receiving data in wrong order, I would suggest using protoc --decode_raw to see whether the error occurs on the transmission or reception side.


1 Answers

Alright, I got this figured out. To read in a text proto file into an object....

#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;
}

My program takes in the input file for the proto buff as the first parameter when i execute it at the terminal. For example ./myProg inputFile.txt

Hope this helps anyone with the same question

like image 151
user1314238 Avatar answered Dec 01 '22 00:12

user1314238