1

I have some struggles in writing a struct of "many variables" to a binary file (as a temporary file) to be read in other models (when needed) under the identical struct format (struct preprocessing_variables)

My struct is as follow:

struct preprocessing_variables{
    parameters params;
    obj_prop objprop;
    griddf grid;
    vector<time_t long long> timevec;
    landmask lmdomain;
    data3D w1_array;
    data3D w2_array;
    data3D w3_array;
    vector<int> orientation;
    position pos;
    cross_wind crosswind;
}

Sub-structs:

struct parameters {
  int N, radius, t_sum, obj;
  double  dt;
  string t0;
  float lon0, lat0, lonn, latn,x0, y0, dl;
};
struct landmask {
    vector<vector<double>> value;
};
struct data3D {
    vector<vector<vector<double>>> u;
    vector<vector<vector<double>>> v;
};
struct position{
    vector<float> x;
    vector<float> y;
};
...

I wanted to write them all into one binary file. What I did is kind of bulky and there are some errors in codes. And I want to simplify this code as well. Please give a hand! I will appreciate.

void save_preprocessing(preprocessing_variables prevars){
    const char* filename="preprocessing.data";
    FILE *outfile;
    if(outfile= fopen(filename,"wb")) fclose(outfile);
    remove(filename);
    outfile= fopen(filename,"wb");
    fwrite(&prevars.params,sizeof(prevars.params),1,outfile);
    fwrite(&prevars.objprop,sizeof(prevars.objprop),1,outfile);
    fwrite(&prevars.grid,sizeof(prevars.grid),prevars.grid.lat.size(),outfile);
    fwrite(&prevars.timevec,sizeof(prevars.timevec),prevars.timevec.size(),outfile); 
    fwrite(&prevars.lmdomain,sizeof(prevars.lmdomain),prevars.lmdomain.value.size(),outfile);
   fwrite(&prevars.w1_array,sizeof(prevars.w1_array),prevars.w1_array.u.size(),outfile);
...
fwrite(&prevars.orientation,sizeof(prevars.orientation),prevars.orientation.size(),outfile);
    fwrite(&prevars.pos,sizeof(prevars.pos),prevars.pos.x.size(),outfile);
    fwrite(&prevars.crosswind,sizeof(prevars.crosswind),1,outfile);
    
    fclose(outfile);
}

  • 1
    Does this answer your question? [How to read / write a struct in Binary Files?](https://stackoverflow.com/questions/5506645/how-to-read-write-a-struct-in-binary-files) – Passerby Oct 22 '21 at 09:56
  • "and there are some errors in codes" We can only help you if you actually ask the question. Please read [ask] and https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question. Specifically what goes wrong when you run the code? What is the result, what should happen instead, and how is that different? – Karl Knechtel Oct 22 '21 at 10:09

1 Answers1

0

I suggest use flat buffer or google protobuf if you want to exchange strong data formatted in enterprise application.

all of them can serialize your class into binary and de-serialize your class in another program.

If you hesitate to choose between them, you can see What's the difference between Protocol Buffers and Flatbuffers?

Dharman
  • 30,962
  • 25
  • 85
  • 135
sorosh_sabz
  • 2,356
  • 2
  • 32
  • 53
  • 2
    What is "ITNOA"? Please write your answers appropriately for a Q&A site (which this is), not for a discussion forum or casual communication. – Karl Knechtel Oct 22 '21 at 10:10