[Solved] c++, reading string from file, segmentation fault


I’ve read Your answers, search in books and changed whole idea of reading-writing 🙂
Now when I write I use:

int stringSize=text.length()*sizeof(char);
file.write(reinterpret_cast<char*>(&stringSize),sizeof(stringSize));
file.write(text.c_str(),stringSize);

And when I read I use:

int stringSize=0;
string text;
plik.read(reinterpret_cast<char*>(&stringSize),sizeof(stringSize));
char * tempCharArray = new char[stringSize+1]{};
plik.read(tempCharArray,stringSize);
text=tempCharArray;
delete [] tempCharArray;

No segmentation fault/access violation since then 🙂
I guess reading “into” (char*)myString.c_str() was a problem, just as Chipster pointed out.
Thanks.

solved c++, reading string from file, segmentation fault