How to save array of struct with vector in binary C++ -
i need save array of struct in binary file. there problem! have 3d array of vector in struct! struct :
struct state { vector<bool> ***value; vector<int> ***rfcclb2bits; state() { new3darray(value , 60 , 100 , 2); new3darray(rfcclb2bits , 60 , 100 , 2); } };
and may want see new3darray function :
template <class t> void new3darray(t ***&u3d, int nx , int ny , int nz) { u3d = new t ** [nx]; if (null == u3d) { cout <<"erro3"<<endl; } else { (int = 0; < nx; i++) { u3d[i] = new t *[ny]; if (null == u3d[i]) { cout <<"erro4"<<endl; } else { (int j = 0; j < ny; j++) { u3d[i][j] = new t [nz]; if (null == u3d[i][j]) { cout <<"erro5"<<endl; } } } } } }
at last code saving , loading :
int main() { state *states; int k ; cin >> k; states = new state[k]; ... string location = "save1.bin"; ofstream fs(location, std::ios::out | std::ios::binary | std::ios::app); fs.write(reinterpret_cast<const char *>(states), sizeof(states)); fs.close(); ... ifstream file (location, ios::in|ios::binary|ios::ate); if (file.is_open()) { file.seekg (0, ios::beg); file.read ((char*)&states, sizeof(states)); file.close(); } ... }
but didnt work! must do?
Comments
Post a Comment