Read/Write Class Objects from/to File in C++ Last Updated : 24 Jan, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a file "Input.txt" in which every line has values same as instance variables of a class. Read the values into the class's object and do necessary operations.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 : // Here file_obj is an object of ofstream file_obj.write((char *) & class_obj, sizeof(class_obj)); To read file's data members into an object : // Here file_obj is an object of ifstream file_obj.read((char *) & class_obj, sizeof(class_obj)); Examples: Input : Input.txt : Michael 19 1806 Kemp 24 2114 Terry 21 2400 Operation : Print the name of the highest rated programmer. Output : Terry C++ // C++ program to demonstrate read/write of class // objects in C++. #include <iostream> #include <fstream> using namespace std; // Class to define the properties class Contestant { public: // Instance variables string Name; int Age, Ratings; // Function declaration of input() to input info int input(); // Function declaration of output_highest_rated() to // extract info from file Data Base int output_highest_rated(); }; // Function definition of input() to input info int Contestant::input() { // Object to write in file ofstream file_obj; // Opening file in append mode file_obj.open("Input.txt", ios::app); // Object of class contestant to input data in file Contestant obj; // Feeding appropriate data in variables string str = "Michael"; int age = 18, ratings = 2500; // Assigning data into object obj.Name = str; obj.Age = age; obj.Ratings = ratings; // Writing the object's data in file file_obj.write((char*)&obj, sizeof(obj)); // Feeding appropriate data in variables str = "Terry"; age = 21; ratings = 3200; // Assigning data into object obj.Name = str; obj.Age = age; obj.Ratings = ratings; // Writing the object's data in file file_obj.write((char*)&obj, sizeof(obj)); //close the file //It's always a good practice to close the file after opening them file_obj.close(); return 0; } // Function definition of output_highest_rated() to // extract info from file Data Base int Contestant::output_highest_rated() { // Object to read from file ifstream file_obj; // Opening file in input mode file_obj.open("Input.txt", ios::in); // Object of class contestant to input data in file Contestant obj; // Reading from file into object "obj" file_obj.read((char*)&obj, sizeof(obj)); // max to store maximum ratings int max = 0; // Highest_rated stores the name of highest rated contestant string Highest_rated; // Checking till we have the feed while (!file_obj.eof()) { // Assigning max ratings if (obj.Ratings > max) { max = obj.Ratings; Highest_rated = obj.Name; } // Checking further file_obj.read((char*)&obj, sizeof(obj)); } // close the file. //It's always a good practice to close the file after opening them file_obj.close(); // Output is the highest rated contestant cout << Highest_rated; return 0; } // Driver code int main() { // Creating object of the class Contestant object; // Inputting the data object.input(); // Extracting the max rated contestant object.output_highest_rated(); return 0; } Output: Terry Comment More infoAdvertise with us Next Article Creating a Vector of Class Objects in C++ R Rohit Thapliyal Improve Article Tags : Misc C++ cpp-input-output cpp-class Practice Tags : CPPMisc Similar Reads RapidJSON - File Read/Write in C++ RapidJSON is a C++ library for parsing and generating JSON (JavaScript Object Notation) data. Â It is designed for high performance and can handle very large JSON documents. RapidJSON supports both reading and writing JSON data, as well as validation and manipulation of JSON objects. It also includes 6 min read Creating a Vector of Class Objects in C++ Prerequisites: Object Oriented Programming in C++Vector in C++ Class is a user-defined data type that can be accessed by creating objects or instances of that class. A vector is a type of container which can store elements of a similar type. Vector of Class The vector of class objects is an example 3 min read Reading Lines by Lines From a File to a Vector in C++ STL Prerequisites: STL in C++Vector in C++File handling in C++ The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrays, etc. It is a library of container classes, algorithms, and iterators. Vector in C 2 min read Printing the Address of an Object of Class in C++ Prerequisite: Classes and Objects in C++ The location of an object in the memory is called its address. Addressing is a necessary part of C++, it enables us to use any element as a reference and maintains the uniqueness of all the elements whether it is any variable, object, or container. In this ar 3 min read Reference to Dynamic Objects in C++ In C++, the objects can be created at run-time. C++ supports two operators new and delete to perform memory allocation and de-allocation. These types of objects are called dynamic objects. The new operator is used to create objects dynamically and the delete operator is used to delete objects dynami 3 min read Storage Classes in C++ with Examples C++ Storage Classes are used to describe the characteristics of a variable/function. It determines the lifetime, visibility, default value, and storage location which helps us to trace the existence of a particular variable during the runtime of a program. Storage class specifiers are used to specif 6 min read Like