根据示例代码https://developers.google.com/protocol-buffers/docs/cpptutorial,它们展示了如何解析二进制格式的原始文件。使用
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;
}
}
我尝试删除文本格式的输入文件的ios::b的
,但在读取文件时仍然失败。我需要做什么才能读取文本格式的原始文件?
只是总结一下要点:
#include <google/protobuf/text_format.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <fcntl.h>
using namespace google::protobuf;
(...)
MyMessage parsed;
int fd = open(textFileName, O_RDONLY);
io::FileInputStream fstream(fd);
TextFormat::Parse(&fstream, &parsed);
在Linux上使用g 4.9.2上的protobuf-3.0.0-beta-1进行检查。
我需要做什么来读取文本格式的原型文件?
使用TextFormat::Parse。我不知道有多少C语言可以提供完整的示例代码,但您应该在这里查找。
好吧,我弄明白了。要将文本原型文件读入对象。。。。
#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输入文件。txt
希望这能帮助任何有同样问题的人
我是协议缓冲区的新手,我想知道是否可以搜索协议缓冲区二进制文件并以结构化格式读取数据。例如,如果我的.proto文件中的一条消息有4个字段,我希望序列化该消息,并将多条消息写入一个文件,然后在该文件中搜索特定的字段。如果我找到该字段,我希望以与写入时相同的结构化格式读回消息。对于协议缓冲区,这是可能的吗?如果可能,任何示例代码或示例都将非常有用。谢谢
问题内容: 我正在尝试动态解析Java中的给定.proto文件,以解码Protobuf编码的二进制文件。 我有以下解析方法,其中“ proto”字符串包含.proto文件的内容: 但是,执行时,先前的方法将引发消息“协议消息标签的电线类型无效”的异常。我使用了来自Google的示例.proto文件,因此我认为它是有效的:https : //github.com/google/protobuf/bl
问题内容: 是否可以在Java中的协议缓冲区? C ++版本具有它:在这里 问题答案: 这是ParseFromString的实现(请注意,只需调用一个新对象): 您可以看到只是清除了消息,然后调用。尽管协议缓冲区的Java实现没有方法,但是您可以轻松实现它:
在阅读这个相当长的问题之前,我提出了一个bughttps://github.com/GoogleCloudPlatform/python-docs-samples/issues/1103. 原型包和名称解析的留档状态 您可以使用其他定义。通过导入原始文件。导入另一个。在proto的定义中,您可以在文件的顶部添加一条import语句。 我的依赖于annotations.proto将HTTP/JSON
(见底部更新) Tilemaker是一个OpenStreetMap程序,用于从OSM pbf数据文件生成Mapbox矢量图块(其本身就是协议缓冲区(pbf)文件)。我已经编译了它,并用它创建了一个矢量平铺目录。我无法用Python解析这些文件。 我使用以下内容创建了矢量平铺: 然后,我以Google的Protocol Buffers python教程为基础,以这种方式创建了一个简单的python程
问题内容: 我正在使用gSoap将旧式C 系统重构为SOA。我们遇到了一些性能问题(非常大的XML),因此我的领导要我看一下协议缓冲区。我做到了,它看起来非常酷(我们需要C 和Java支持)。但是协议缓冲区是仅用于序列化的解决方案,现在我需要将其发送到Java前端。从C ++和Java角度来看,我应该使用什么来通过HTTP(只是内部网络)发送那些序列化的内容? PS。另一个人试图加速我们的gSoa