Saving and loading data into a C ++ file (beginner)

I have a class containing many different variables, for example, there are several multidimensional vectors there.

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

For example, if I create an instance of this class, fill it in and then save it to a file, can I load it in the same way? How it works? Am I just saving everything in one go or do I need to split the data somehow?

+4
source share
3 answers

This is not entirely new to C ++

C ++ does not have an automated way to store / load your objects to / from a file. In any case, you decide to go, you have to implement it yourself.

You might want to overload the << a >> statements for use with streams, or you can go with your Load and Store methods (or any names you choose as appropriate, for example Serialize / Deserialize ). I personally prefer to create my own functions rather than using operators, but that's just me.

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

 #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 is worth mentioning that you must take care of the serialization format. In the above example, this is just text; but if you are going to store a lot of such objects, you can start thinking about serializing binary data (you will need to use the flags ofstream::binary and ifstream:binary when opening files and not need additional delimiters, for example, ' ' and endl in your serialization stream )

As a rule, when you think about serialization, you also want to think about the version of your stream - and this is another separate topic.

+4
source

You might consider introducing stream statements.

With them, you can simply use the following for reading and writing:

 fileStream >> yourObject fileStream << yourObject 

Basically in the operator → you will read the stream and build an object with the data found. In the <statement, you write the object to the stream in the same format that you want to read it.

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

Google for “congestion flow statements” to know how to implement these statements.

0
source

You can either provide code that will iterate through all the members of the class (optionally skipping the unimportant and transforming some others), and prepare a continuous stream of data. This serialization is in a narrower sense. The same thing needs to be done in reverse lookups.

In C ++, you can also perform a binary dump, for example CopyMemory(ptr, sizeof(*ptr) . It can only work if your data does not contain pointers (especially with regard to the hidden pointer for classes with virtual methods). Its only advantages are simplicity and tremendous speed.This approach requires your data to be contiguous in memory, which is sometimes useful for it.

0
source

All Articles