write struct with string in binary file

Hi!

here is my problem. I have a structure :

struct Entrainement{
string muscleCible, nom, unitesObjectif;
double objectifActuel, dernierObjectif, objectifInitial, capaciteInitiale, progression[10];
};



and I want to write in a binary file the structure. The following works perfectly....


(*pointeurFichierEntrainementBin).write((char*)&exercice, sizeof(Entrainement));




...except when the string exceed 11 characters. I guess it's because it has to pick a fixed sized for the string? but what if I want to always be able to have string up to 200 character? because now I can't exceed 11...

I know writing a string with c_str() works, but I would like to write/read the structure in one shot

thx
No, you cannot do that. std::string is not a POD type and so you cannot just blindly read/write its memory - you have to treat it separately. You got lucky with less than 11 characters because your standard library implementation uses the short string optimization.

C++ is not C - do not treat it like C with classes.
Last edited on
Topic archived. No new replies allowed.