协议缓冲区:如何将多个消息序列化和反序列化为文件(C ++)?

use*_*746 4 c++ structure protocol-buffers

我是Protocol Buffers和c ++的新手,但我的任务要求我同时使用两者。我想多次将数据(消息)的结构写入单个文件,并能够读取数据。我可以读写一条消息,但事实证明多条消息很难。我已经寻找了几个小时的答案,但似乎无法将数据作为结构读取。任何示例代码或指针将非常有帮助。

这是我的结构的格式:

    typedef struct Entry
       {
          char  name[ NAME_MAX];
          int   id;
          int   age;
          char  DoB[32]; 
       } entry;
Run Code Online (Sandbox Code Playgroud)

这就是我一直用来写入文件的内容:

    Person_File file;
    fstream output("file", ios::out | ios::trunc | ios::binary);
    file.SerializeToOstream(&output);
Run Code Online (Sandbox Code Playgroud)

我试图将文件编辑选项更改为追加而不是截断,但这不能让我以所需的格式读取数据。

这是我用来阅读的内容:

    fstream input("file", ios::in | ios::binary);
    file.ParseFromIstream(&input);
Run Code Online (Sandbox Code Playgroud)

这些是我的.proto文件的内容:

    message Person {


message File {
    required string pname =1;
    required int32 id =2;
    required int32 age =3;
    required string DoB =4;
}

repeated File file =1;
Run Code Online (Sandbox Code Playgroud)

}

从我完成的所有搜索中,似乎CodedInputStream / CodedOutputStream是我最好的选择,但我无法找到任何详细的示例或解释来帮助我理解。我了解协议缓冲区不是自定界的,这可能就是为什么我无法以原始格式读回消息的原因。任何帮助,将不胜感激。谢谢

编辑:我已尝试根据收到的消息使用CodedOutputStream,但似乎未在文件中写入任何内容。

    int fd = open("file.txt",O_WRONLY | O_APPEND | O_CREAT);

    FileOutputStream* file_ostream_ = new FileOutputStream(fd);
    CodedOutputStream* ostream_ = new CodedOutputStream(file_ostream_);


ostream_->WriteLittleEndian32(file.ByteSize());
    file.SerializeToCodedStream(ostream_);
Run Code Online (Sandbox Code Playgroud)

使用此代码后,文件变为空白。我要去哪里错了?

Ken*_*rda 5

我曾尝试根据收到的消息使用CodedOutputStream,但似乎未在文件中写入任何内容。

我怀疑您的问题是您没有删除CodedOutputStreamFileOutputStream。这些对象缓冲输出并刷新其析构函数中的缓冲区,因此,如果您从不销毁它们,它们将不会写入最后一个缓冲区,在这种情况下,这是唯一的缓冲区。

我建议在堆栈上分配这些对象(作为局部变量)。这样您就不可能忘记销毁它们。

顺便说一句,这是使用CodedInputStreamCodedOutputStream读取/写入定界消息的代码。每条消息均以表示大小的varint开头,该大小与Java Protobuf库的writeDelimitedTo()/ 格式相同parseDelimitedFrom()(此功能从未进入C ++库)。

bool writeDelimitedTo(
    const google::protobuf::MessageLite& message,
    google::protobuf::io::ZeroCopyOutputStream* rawOutput) {
  google::protobuf::io::CodedOutputStream output(rawOutput);

  // Write the size.
  const int size = message.ByteSize();
  output.WriteVarint32(size);

  uint8_t* buffer = output.GetDirectBufferForNBytesAndAdvance(size);
  if (buffer != NULL) {
    // Optimization:  The message fits in one buffer, so use the faster
    // direct-to-array serialization path.
    message.SerializeWithCachedSizesToArray(buffer);
  } else {
    // Slightly-slower path when the message is multiple buffers.
    message.SerializeWithCachedSizes(&output);
    if (output.HadError()) return false;
  }

  return true;
}

bool readDelimitedFrom(
    google::protobuf::io::ZeroCopyInputStream* rawInput,
    google::protobuf::MessageLite* message) {
  google::protobuf::io::CodedInputStream input(rawInput);

  // Read the size.
  uint32_t size;
  if (!input.ReadVarint32(&size)) return false;

  // Tell the stream not to read beyond that size.
  auto limit = input.PushLimit(size);

  // Parse the message.
  if (!message->MergePartialFromCodedStream(&input)) return false;
  if (!input.ConsumedEntireMessage()) return false;

  // Release the limit.
  input.PopLimit(limit);

  return true;
}
Run Code Online (Sandbox Code Playgroud)