简体   繁体   中英

How to read and write class with dynamic array member from/to binary file C++

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

What you really want is some serialization library. And you probably want to use containers , so you would serialize some std::vector<double> for a vector, and you would define your own template matrix<typename ElementType> (or better yet find one, perhaps in Boost , which fits your needs). You certainly need to define the abstract data type for your matrixes (so the collection of all the operations on them). BTW, this answer (for C) and that one could inspire you.

You obviously should not write raw pointers in a file, because they make sense only for your process . Even if you run the same program in the same state (eg same matrixes) in two different processes, they would use different pointer addresses (eg because of ASLR ). You want to write (and later read) the contents of your matrixes.

You might be interested in existing serialization libraries such as s11n , or at least in neutral binary formats such as XDR or ASN1 . You probably should care about endianess (eg if reading on a Sparc or ARM computer a binary file written on x86).

You could decide to serialize in some textual format (or text-based protocol ), such as JSON (or YAML, etc...). This is more friendly to developers (and for debugging). You can find a lot of JSON related libraries for C++, eg jsoncpp .

If you are not allowed to use external libraries, you could reinvent and document (perhaps inEBNF notation) your textual serialization format (inspired by JSON, or by S-exprs ) and easily implement it (using usual parsing techniques, such as recursive descent parser ).

If you are required to use a binary format, you need to specify it (again EBNF notation should help) and implement it (again, it is a parsing problem for the decoding; encoding is usually much simpler).

Very probably, you want to encode (and decode) each matrix element one by one. That is not a big deal, once your abstract data type has the operations to fetch (and perhaps to change) the value of some individual matrix element.

Read alsoHow to debug small programs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM