Search code examples
c++c++11fstreamstdlist

std::list doesn't initialize correctly after it's content gets read from fstream


To explain briefly: Using <fstream>, I write a std::list instance to a .txt file:

#include <fstream>
#include <list>

std::list<Item> list_1; //example list
list_1.push_back(Item(...));

std::ofstream file;
file.open("record.txt", std::ios::trunc);

if (file.is_open()) {
    file.write((char*)&list_1, sizeof(std::list<Item>)) << std::endl;

    file.close();
}

However, when I read from the same file and assign the data to a std::list instance:

file.open("record.txt", std::ios::in);
if (file.is_open()) {
    std::list<Item> list_1;
    file.read((char*)&list_1, sizeof(std::list<Item>));
}

It gives me an error when I try to access its elements. This is, however, not my problem. Because std::list stores the pointer to that element, I must store the elements manually, like I did here:

for (auto const& item : list_1) {
    file << item.amount << std::endl;
    file << item.value << std::endl;
    file << item.item_name << std::endl;
    file << (char*)&item.type << std::endl;
}

Then I read these values. Use the values to create a new Item instance and store it inside my list. Side note: I can access the size() property of the list_1 from the .txt file because it is a member of std::list<Item> which lives on the stack. So it gets saved by the ofstream.

for (int i = 0; i < list_1.size(); i++) {
    int amount = 0;
    int value = 0;
    std::string item_name;
    Item_Type type = item;

    file >> amount;
    file >> value;
    file >> item_name;
    file >> (char*)&type;

    Item item(amount, value, item_name, type);
    main_player_inv.push_back(item);

I expect this to work, because now the std::list should have no uninitialized members, right?

Well, it gives me this error:

this->_Mypair._Myval2._Myhead was 0x228B4050240

This basically means list_1->_Mypair._Myval2._Myhead is a pointer which points to memory out of bounds. The problem is, unlike the element pointers which I can manually save the values of and initialize, I can't access the data of list_1->_Mypair._Myval2._Myhead or edit it manually, as it is a private member. Or, there isn't a way I could find online.

So, I have two questions:

  • Can I initialize list_1->_Mypair._Myval2._Myhead so that it points to a valid memory?

  • Is there a way to more easily serialize a std::list and retrieve it's content?

If both of these questions are unanswerable, I would like to talk about what I'm trying to do:

The std::list<Item> is used as a character or an object's inventory. In my project, I want to store the items the player and objects such as containers have in a std::list<Item> instance. I thought this was the most fitting thing to do for an object-oriented Player structure. Here are some classes, for example:

Player class

class Player : public Object {
public:
    int level, experience;
    double health;
    float resistance; // 0.000 - 1.000
    std::list<Item> inventory;
public:
    Player() :
        level(0), experience(0), health(10.0), resistance(0.0f) {};
    Player(int x, int y, std::string obj_name, Obj_Type type, int level, int experience, double health, float resistence) :
        Object(x, y, obj_name, type), level(level), experience(experience), health(health), resistance(resistence) {};
};

Item class

struct Item {
public:
    unsigned int amount, value;
    std::string item_name;
    Item_Type type;  // enum
public:
    Item() :
        amount(0), value(0), item_name("undefined"), type(item) {};
    Item(int amount, int value, std::string item_name, Item_Type type) :
        amount(amount), value(value), item_name(item_name), type(type) {};
};

If you know a better way to store player items, items being class instances; or know altogether a better way to do this, please help me.


Solution

  • You can't read/write the raw bytes of a std::list object (or any other non-trivial type), as you would be writing/reading raw pointers and other internal data members that you don't need to concern yourself with.

    You must (de)serialize your class's individual data members instead, as you have already discovered.

    I would suggest a binary format instead of a textual format, eg:

    #include <type_traits>
    
    template <typename T, std::enable_if_t<std::is_scalar<T>::value, bool> = true>
    void writeToStream(std::ostream &out, const T &value) {
        out.write(reinterpret_cast<const char*>(&value), sizeof(value));
    }
    
    template <typename T, std::enable_if_t<std::is_scalar<T>::value, bool> = true>
    void readFromStream(std::istream &in, T &value) {
        in.read(reinterpret_cast<char*>(&value), sizeof(value));
    }
    
    void writeToStream(std::ostream &out, const std::string &value) {
        size_t size = value.size();
        writeToStream(out, size);
        out.write(value.c_str(), size);
    }
    
    void readFromStream(std::istream &in, std::string &value) {
        size_t size;
        readFromStream(in, size);
        value.resize(size);
        in.read(value.data() /* or: &value[0] */, size);
    }
    
    template <typename Container>
    void writeToStream(std::ostream &out, const Container &items) {
        size_t count = items.size();
        writeToStream(out, count);
        for(const auto& item : items) {
            writeToStream(out, item);
        }
    }
    
    template <typename Container>
    void readFromStream(std::istream &in, Container &items) {
        size_t count;
        readFromStream(in, count);
        items.reserve(count);
        for(size_t i = 0; i < count; ++i) {
            Container::value_type item;
            readFromStream(in, item);
            items.push_back(item);
        }
    }
    
    template<typename Container>
    void writeToFile(const std::string &fileName, const Container &items) {
        std::ofstream file(fileName, std::ios::binary);
        file.exceptions(std::ofstream::failbit);
        writeToStream(file, items);
    }
    
    template<typename Container>
    void readFromFile(const std::string &fileName, Container &items) {
        std::ifstream file(fileName, std::ios::binary);
        file.exceptions(std::ifstream::failbit);
        readFromStream(file, items);
    }
    
    struct Item {
    public:
        unsigned int amount, value;
        std::string item_name;
        Item_Type type;  // enum
    public:
        Item() :
            amount(0), value(0), item_name("undefined"), type(item) {};
        Item(int amount, int value, std::string item_name, Item_Type type) :
            amount(amount), value(value), item_name(item_name), type(type) {};
    
        void writeTo(std::ostream &out) const {
            writeToStream(out, amount);
            writeToStream(out, value);
            writeToStream(out, item_name);
            writeToStream(out, type);
        }
    
        void readFrom(std::istream &in) {
            readFromStream(in, amount);
            readFromStream(in, value);
            readFromStream(in, item_name);
            readFromStream(in, type);
        }
    };
    
    void writeToStream(std::ostream &out, const Item &item) {
        item.writeTo(out);
    }
    
    void readFromStream(std::istream &in, Item &item) {
        item.readFrom(in);
    }
    
    class Player : public Object {
    public:
        int level, experience;
        double health;
        float resistance; // 0.000 - 1.000
        std::list<Item> inventory;
    public:
        Player() :
            level(0), experience(0), health(10.0), resistance(0.0f) {};
        Player(int x, int y, std::string obj_name, Obj_Type type, int level, int experience, double health, float resistence) :
            Object(x, y, obj_name, type), level(level), experience(experience), health(health), resistance(resistence) {};
    
        void writeTo(std::ostream &out) const {
            writeToStream(out, level);
            writeToStream(out, experience);
            writeToStream(out, health);
            writeToStream(out, resistance);
            writeToStream(out, inventory);
        }
    
        void readFrom(std::istream &in) {
            readFromStream(in, level);
            readFromStream(in, experience);
            readFromStream(in, health);
            readFromStream(in, resistance);
            readFromStream(in, inventory);
        }
    };
    
    void writeToStream(std::ostream &out, const Player &player) {
        player.writeTo(out);
    }
    
    void readFromStream(std::istream &in, Player &player) {
        player.readFrom(in);
    }
    
    #include <fstream>
    #include <list>
    
    int main() {
        std::list<Item> list_1; //example list
        list_1.push_back(Item(...));
    
        writeToFile("record.txt", list_1);
    
        list_1.clear();
    
        readFromFile("record.txt", list_1);
        
        return 0;
    }
    

    If you really want a textual file, then use operator<< and operator>> instead, overriding them in your classes, eg:

    (feel free to tweak this to use whatever formatting you want...)

    #include <limits>
    
    void discardLine(std::istream &in) {
        in.ignore(std::numeeric_limits<std::streamsize>::max(), '\n');
    }
    
    template<typename CharT, typename Traits>
    void streamFailed(std::basic_ios<CharT,Traits> &stream) {
        stream.setstate(std::ios_base::failbit);
    }
    
    template <typename Container>
    std::ostream& operator<<(std::ostream &out, const Container &items) {
        out << '[' << items.size() << '\n';
        for(const auto& item : items) {
            out << item << '\n';
        }
        out << ']\n';
        return out;
    }
    
    template <typename Container>
    std::istream& operator>>(std::istream &in, Container &items) {
        char ch;
        in >> ch;
        if (ch != '[') {
            streamFailed(in);
        } else {
            size_t count;
            in >> count;
            discardLine(in);
            items.reserve(count);
            for(size_t i = 0; i < count; ++i) {
                Container::value_type item;
                in >> item;
                items.push_back(item);
            }
            in >> ch;
            if (ch != '[') {
                streamFailed(in);
            }
        }
    }
    
    template<typename Container>
    void writeToFile(const std::string &fileName, const Container &items) {
        std::ofstream file(fileName, std::ios::binary);
        file.exceptions(std::ofstream::failbit);
        file << items;
    }
    
    template<typename Container>
    void readFromFile(const std::string &fileName, Container &items) {
        std::ifstream file(fileName, std::ios::binary);
        file.exceptions(std::ifstream::failbit);
        file >> items;
    }
    
    struct Item {
    public:
        unsigned int amount, value;
        std::string item_name;
        Item_Type type;  // enum
    public:
        Item() :
            amount(0), value(0), item_name("undefined"), type(item) {};
        Item(int amount, int value, std::string item_name, Item_Type type) :
            amount(amount), value(value), item_name(item_name), type(type) {};
    
        friend std::ostream& operator<<(std::ostream &out, const Item &item) {
            out << '(' << item.amount << ' ' << item.value << ' ' << static_cast<int>(item.type) << ' ' << item.item_name << ')';
            return out;
        }
    
        friend std::istream& operator>>(std::istream &in, Item &item) {
            char ch;
            in >> ch;
            if (ch != '(') {
                streamFailed(in);
            } else {
                int itype;
                in >> item.amount >> item.value >> itype;
                item.type = static_cast<Item_Type>(itype);
                std::getline(in >> std::ws, item_name, ')');
            }
            return in;
        }
    };
    
    class Player : public Object {
    public:
        int level, experience;
        double health;
        float resistance; // 0.000 - 1.000
        std::list<Item> inventory;
    public:
        Player() :
            level(0), experience(0), health(10.0), resistance(0.0f) {};
        Player(int x, int y, std::string obj_name, Obj_Type type, int level, int experience, double health, float resistence) :
            Object(x, y, obj_name, type), level(level), experience(experience), health(health), resistance(resistence) {};
    
        friend std::ostream& operator<<(std::ostream &out, const Player &player) {
            out << '(' << level << ' ' << experience << ' ' health << ' ' << resistance << '\n';
            out << inventory;
            out << ')';
            return out;
        }
    
        friend std::istream& operator>>(std::istream &in, Player &player) {
            char ch;
            in >> ch;
            if (ch != '(') {
                streamFailed(in);
            } else {
                in >> player.level >> player.experience >> player.health >> player.resistance >> player.inventory;
                in >> ch;
                if (ch != ')') {
                    streamFailed(in);
                }
            }
            return in;
        }
    };
    
    #include <fstream>
    #include <list>
    
    int main() {
        std::list<Item> list_1; //example list
        list_1.push_back(Item(...));
    
        writeToFile("record.txt", list_1);
    
        list_1.clear();
    
        readFromFile("record.txt", list_1);
        
        return 0;
    }