The problem: I'm having a problem with reading and writing files in binary mode using C++ stl only. What I have in my class are 2 integer member to tell me the matrix's size and the actual matrix itself. The matrix is dynamic though which I think is the main reason why non of my read request is correct. So my question is how do I read/write for class with dynamic arrays member.
What I've tried: reading in the size of the matrix before hands and then allocate memory for the matrix array. But for some reason my debugger just keeps saying it's segmentation fault although I have made some room for it. I've even tried to make two class with the same member values, still nothing
class MaTrix{
private :
int row, column;
float ** maTrix_arr;
}
void outFile(){
ofstream of("matrix.inp", ios::binary | ios::app) ;
of.write(reinterperted_cast<char*>(this), sizeof(*this)) ;
}
void inFile(){
ifstream ifs("matrix.inp", ios::binary | ios::app) ; // if I don't use app it will delete my file for some reason
// I do some more allocation here to make sure the Matrix is of suitable size
// Long code short I tried to get the two integers row and column out and it works as intended
// Then I allocate some memory for this->maTrix_arr, also making sure I reset the file pointer
ifs.read(reinterperted_cast<char*>(this), sizeof(*this)) ;
}
};