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);
}