1 year ago

#380645

test-img

Boris

Loading Protocol Buffer From File Does Not Succeed

I want to load Google Protocol Buffer encoded data from a file via "ParseFromIstream". But this call simply returns false (no additional error information available?) and the data is only read partially (e.g. various sub-messages are empty).

When I load the data to memory beforehand and then use "ParseFromArray", the parser succeeds and all data is present.

I would like to find out why ParseFromIstream fails and how to fix this behavior.

Here's my example code:

// Test 1 - read data from memory -> works

// Read data from file to memory
int numBytes;
char* bytes = ReadFileToMemory(L"C:\\MyData.bin", &numBytes);

// Parse protobuf
mytest::ProtoRoot root1{};
bool loadFromMemoryOk = root1.ParseFromArray(bytes, numBytes); // OK
// Parsing worked, root1 contains all data



// Test 2 - read data from file -> does not work

// Open file stream
std::ifstream ifs(L"C:\\MyData.bin", std::ifstream::in);

// Parse protobuf
mytest::ProtoRoot root2{};
bool loadFromFileOk  = root2.ParseFromIstream(&ifs); // Not OK
// Parsing failed, root2 only contains some data, most fields are empty

c++

protocol-buffers

ifstream

0 Answers

Your Answer

Accepted video resources