fstream doesn't work in combination with linked lists

Hello Dear Community,
i have got a big problem. Im about to program a RPG and, of course, the player has got an inventory. The items in this inventory are stored in a linked list and have IDs in form of strings.

player.cpp: (just the most relevant)
1
2
3
4
5
6
7
8
9
10
#include <list>
#include <string>

using namespace std;

class CPlayer {
friend class CGame;
private:
list <string> m_inventory;
}


Of course, when the player saves the game, the inventory has to be saved too.

game.cpp(just the m. R.)
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <fstream>

using namespace std;

class CGame{
private:
ofstream m_out;
ifstream m_in;

public:
void save (CPlayer* player);
void load (CPlayer* player);
}


Now, in my code I write the linked list into the file savegames.sav
1
2
3
4
5
6
7

void CGame::save(CPlayer* player){
m_out.open("savegames.sav", ios::out);
m_out.write((char* ) &player->m_inventory, sizeof(player->m_inventory));
m_out.close();

}


In another function i load the list:

1
2
3
4
5
void CGame::load(CPlayer* player){
m_in.open("savegames.sav", ios::in);
m_in.read((char* ) player->m_inventory, sizeof(player->m_inventory));
m_in.read();
}


And when i access the loaded linked list with the .empty() function or so,
Windows says the following:
<Mygame.exe> funktioniert nicht mehr (in English: doesnt run anymore).

I tried so much to fix that but i dint reach my aim. Please help me, cause if this evil problem was the reason for me to must stop my project, id be very sad.
closed account (3hM2Nwbp)
Casting the binary representation of a linked list and writing it out should technically work1..however when reading it back in, you're most likely writing to memory that you don't own, as the empty list is smaller in size than what you're trying to put in it.

Try iterating the list and writing out the individual strings.

1 - won't do what you expect, but shouldn't cause any indeterministic behavior.
Last edited on
Have you tried to look at the file you wrote using a text editor?

Using m_out.write(...) doesn't save anything useful to your file. You need to write the individual strings to file. To do that, you must decide how you plan to store them in the file.

Since you are only saving a single player's info to the file, you might as well do it the simplest way possible, and save one string per line.

Also, there is no need to keep m_out and m_in in your CGame class.

1
2
#include <algorithm>
#include <iterator> 
1
2
3
4
5
6
7
8
void CGame::save(CPlayer* player){
  std::ofstream m_out("savegames.sav");
  std::copy(
    player->m_inventory.begin(),
    player->m_inventory.end(), 
    std::ostream_iterator <string> (m_out, "\n")
  );
}
1
2
3
4
5
6
7
void CGame::load(CPlayer* player){
  player->m_inventory.clear();
  std::ifstream m_in("savegames.sav");
  std::string s;
  while (std::getline(m_in, s))
    player->m_inventory.push_back(s);
}

Notice that you don't have to explicitly close the file, because it does that automatically when the fstream goes out of scope.

Hope this helps.
Topic archived. No new replies allowed.