In this article we will discuss how to write objects to file and how to read them back from to file and fill in object.

Suppose we have a class Student i.e.

Writing objects in File | Serializing Objects

To write an object in file we need to over load << operator for the file as friend function i.e.

Now we can create Student class objects and store them in file i.e.

Contents of file students.txt will be,

Reading objects from File | Deserializing Objects

To read an object in file we need to overload >> operator for the file as friend function i.e.

Now we can load data in Student class objects from file i.e.

Checkout complete example as follows,

To compile the above example in linux use following command,

g++ –std=c++11 example.cpp

Click Here to Subscribe for more Articles / Tutorials like this.