Saving and loading data to a file c++ (beginner)

32,797

Solution 1

This is not exactly beginner's topic in C++

C++ has no automated way to store / load your objects to / from a file. Either way you chose to go, you'll have to implement it yourself.

You might chose to overload the << an >> operators to use with streams, or you might want to go with your own Load and Store methods (or whatever names you chose appropriate, like, Serialize / Deserialize). I personally prefer to create my own functions and not to use the operators, but it's just me.

Here is a simple example (with overloaded << and >> operators):

#include <fstream>
#include <iostream>

using namespace std;

class MyClass
{
public:
    MyClass (int x) : m_x(x), m_y(x+1) {}

    friend istream& operator >> (istream& in, MyClass& obj);
    friend ostream& operator << (ostream& out, const MyClass& obj);

private:
    int m_x;
    int m_y;
};

istream& operator >> (istream& in, MyClass& obj)
{
    in >> obj.m_x;
    in >> obj.m_y;
    return in;
}

ostream& operator << (ostream& out, const MyClass& obj)
{
    out << obj.m_x << ' ';
    out << obj.m_y << endl;
    return out;
}

int main(int argc, char* argv[])
{
    MyClass myObj(10);
    MyClass other(1);
    cout << myObj;
    ofstream outFile ("serialized.txt");
    outFile << myObj;
    outFile.close();
    ifstream inFile ("serialized.txt");
    inFile >> other;
    inFile.close();
    cout << other;
    return 0;
}

It worth to mention that you should take care about the serialization format. In the example above it's just text; but if you are going to store a lot of those objects, you might start thinking about serializing binary data (you'll need to use ofstream::binary and ifstream:binary flags while opening the files, and will not need additional separators, like ' ' and endl in your serialization stream).

Typically, when you think of serialization, you also want to think of the versioning of your stream -- and this is another separate topic.

Solution 2

You can either provide a code that will iterate through all members of a class (optionally skipping the non-important ones and converting some others) and prepare continuous stream of data. That's serialization in the narrower sense. Ofc the same thing needs to be done in reverse when reading.

In C++ you can also perform a binary dump, like CopyMemory(ptr, sizeof(*ptr). It can only work if your data contain no pointers (that especially includes hidden pointer for classes with virtual methods). Its only upsides are simplicity and tremendous speed. This approach requires your data to be contiguous in memory what sometimes is beneficial on it's own.

Solution 3

You could consider implementing stream operators.

With these you could simply use the following to read and write:

fileStream >> yourObject
fileStream << yourObject 

Basically in the >> operator, you will read the stream and construct the the object with the found data. In the << operator, you write the object to the stream in the same format as you expect to read it.

Using this way of working, you can serialize any object you have.

Google for "overload stream operators" to know how to implement these operators.

Share:
32,797
Sebastian Zander
Author by

Sebastian Zander

Updated on July 08, 2020

Comments

  • Sebastian Zander
    Sebastian Zander almost 4 years

    I have a class containing many different variables, for example there's a few multidimensional vectors in there.

    I've heard that you can store and load data directly to a file, but to what extent?

    For example if I create an instance of this class, fill it up, then save it to a file, can I load it the same way? Like how does that work? Do I just save it all in one go or do I have to split up the data somehow?