Read/Write the entire class variable into a file
Ref: Read/Write Class Objects from/to File in C++
Theory:
The data transfer is usually done using >>
and <<
operators.
But if you have a class with 4 data members and want to write all 4 data members from its object directly to a file or vice-versa, we can do that using following syntax:
To write object's data members in a file:
file_obj.write((char *) & class_obj, sizeof(class_obj));
To read file's data members into an object:
file_obj.read((char *) & class_obj, sizeof(class_obj));