FlatBuffers: write and read from binary?

I have basic knowledge of file streams in C ++ and Google FlatBuffers . The schema file is quite simple, a buffer is also created and read from the buffer pointer. What I am not getting is to save several buffers to a binary, and then read this binary to get an arbitrary buffer.

Here is a simple diagram with two arrays of floats:

table Car { field_a:[float]; field_b:[float]; } 

.

Function for creating a buffer (although without saving the file):

 bool save_flatbuf(string file_path, vector<double> vec_a, vector<double> vec_b) { flatbuffers::FlatBufferBuilder builder; auto vec_floats_a = builder.CreateVector(vec_a, vec_a.size()); auto vec_floats_b = builder.CreateVector(vec_b, vec_b.size()); auto mloc = CreateCar(builder, &vec_floats_a, &vec_floats_b); builder.Finish(mloc); // How to save it into a binary file with a list of "Cars"? } 

.

And the function to read the buffer after reading it from a binary file (without reading the file):

 bool read_flatbuf(string file_path) { // How to get the buffer pointer to a "Car" from a binary file with a "list of Cars" ? . vector<double> final_vec_a; vector<double> final_vec_b; auto car = GetCar(buffer_pointer); auto fa = car->field_a(); auto fb = car->field_b(); final_vec_a.resize(fa->size()); for (int i = 0; i < fa->size(); i++) { final_vec_a[i] = fa->Get(i); } final_vec_b.resize(fb->size()); for (int i = 0; i < fb->size(); i++) { final_vec_b[i] = fb->Get(i); } } 

Not sure if access to buffer information is correct. For example, a way to get the length of the fields in an array.

Code examples for interacting with a file (writing / reading several buffers in one file) are welcome.

+8
c ++ serialization binaryfiles fstream flatbuffers
source share
4 answers

The best way to do this is to add this list of cars to your circuit:

 table Garage { cars:[Car]; } 

Then you can collect several automobile offsets (from CreateCar ), call CreateVector on them, call CreateGarage on it, and then submit the result of this to Finish .

To read, start the same way with GetGarage(buffer_pointer) .

+2
source share

My solution adds additional size information.

for a writer ::

 for (item : flatbuffer_list) { int size = item.GetSize(); write (file, &size, sizeof(size)); write (file, item.GetBufferPointer(), item.GetSize()); } 

for the reader ::

 while(!eof(file)) { int size; read (file, &size, sizeof(size)); read (file, buffer, size); auto item = GetItem(buffer); } 
+2
source share

A quick reference to saving a buffer to a binary file.

 builder.Finish(mloc); uint8_t *buf = builder.GetBufferPointer(); int size = builder.GetSize(); std::ofstream ofile("data.bin", std::ios::binary); ofile.write((char *)buf, size); ofile.close(); 
0
source share

"Code examples for interacting with a file (writing / reading several buffers in one file) are welcome."

I am using fbs and json for this test game. (generate in out_cpp folder: gamedata.bin, gamedata.h)

flatc -b -c -o out_cpp gamedata.fbs gamedata.json

and I found this pattern of flat buffers, which is very useful for the first time.

https://github.com/gene-hightower/fb

In my case, the git sample does not work correctly unless you use flatbuffers :: LoadFile () instead of the sample provided by load_file ().

-one
source share

All Articles