Ok so i have this code i just want to write my struct to the file and then read using another variable because i want to return a vector of userRankings when reading.
Here'es my write/read
void IOManager::WriteBin(const string &filename, userRank u1) {
ofstream fsalida(filename, ios::out | ios::binary); //obrim un archiu per escriure en binari i de tipo append per poder escriure al final i no xafar-ho tot cada cop
if (fsalida.is_open())
{
fsalida.write(reinterpret_cast<char*>(&u1), sizeof(u1));
fsalida.close();
}
else cout << "Unable to open file for writing\n";
}
void IOManager::ReadBin(const string &filename) {
ifstream fentrada(filename, ios::in | ios::binary); //ate per posarnos al final del archiu i tenir el tamany
if (fentrada.is_open())
{
userRank tempUser;
fentrada.read(reinterpret_cast<char*>(&tempUser), sizeof(tempUser));
fentrada.close();
cout << sizeof(tempUser) << endl;
}
else cout << "Unable to open file for reading\n";
}
And my userRank:
struct userRank
{
std::string userName;
int score;
};
The line that fails is fentrada.read(reinterpret_cast(&tempUser), sizeof(tempUser));
Please help, this seems to work with ints, chars, etc but not with strings and complex types, does anybody know why?