gpt4 book ai didi

c++ - 将 protobuf 字节类型存储在二进制文件中

转载 作者:行者123 更新时间:2023-11-30 04:47:34 29 4
gpt4 key购买 nike

我正在尝试拆分和合并一个二进制文件,出于与这个问题无关的原因,我正在使用 protobuf 以 protobuf 字节类型存储文件 char*

序列化 char* 的代码如下所示:

char* buffer = new char[buffer_size];
ifstream fileStream;

fileStream.open(fileName,ios::in | ios::binary);

//stuff here

Data *data = new Data(); // Protobuf Constructor

fileStream.read(buffer, buffer_size);
data->set_payload(buffer);
data->set_piece_no(piece_no);

.proto 文件:

 syntax = "proto3";
message Data {
int32 piece_no = 1;
bytes payload = 2;
}

然后我尝试像这样组合所有部分:

ofstream fileOutput;
fileOutput.open("out.bin", ios::out | ios::binary);
fileOutput << data->payload();

但遗憾的是,这不起作用,生成的二进制文件比原始文件小得多。

然后我怀疑这些字节可能包含空字符\0,结果字节实际上被截断了。

为了检验我的假设,我做了以下事情:

Data *data = new Header();
data->set_payload("hel\0lo");
data->set_piece_no(piece_no);

ofstream fileOutput;
fileOutput.open("out.bin",ios::out | ios::binary);
fileOutput << data->payload();

在文本编辑器 (vscode) 中打开二进制文件显示如下:

hel

但是下面的代码:

string data("hel\0lo",6);
ofstream fileOutput;
fileOutput.open("out.bin", ios::out | ios::binary);
fileOutput << data;

显示以下内容:

hel?lo

我如何才能准确输出我输入到 protobuf 中的内容,而不会因为任意空字节而被截断?

最佳答案

如果你传递一个字符串文字,那么它会这样对待它并且只读到第一个空终止符。

相反,您可以像上一个示例一样直接传递 std::string

参见 https://developers.google.com/protocol-buffers/docs/reference/cpp-generated#oneof 中的“单数字符串字段 (proto3)”

关于c++ - 将 protobuf 字节类型存储在二进制文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56194336/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com