This is the problematic line.
iFile.read((char *)&Name, StringLength);
You are reading the char*
part of a std::string
directly into the memory of Name
.
You need to save both the size of the string as well as the string so that when you read the data, you would know how much memory you need to read the data.
Instead of
oFile.write(Name.c_str(), StringLength);
You would need:
size_t len = Name.size();
oFile.write(&len, sizeof(size_t));
oFile.write(Name.c_str(), len);
On the way back, you would need:
iFile.read(&len, sizeof(size_t));
char* temp = new char[len+1];
iFile.read(temp, len);
temp[len] = '\0';
Name = temp;
delete [] temp;