Read/Write Class Objects from/to File in C++

C++Server Side ProgrammingProgramming

The iostream standard library has two methods cin, to accept input from standard input stream and cout to print output to the standard output stream. In this article we will learn how to read data from files into class objects and how to write data in class objects to files.

Reading and writing data to and from files requires another standard library of C++ <fstream>. The three main data types of fstream are −

  • ifstream − represents input file stream and reads information from files.
  • ofstream − represents output file stream and writes information to files.
  • fstream − represents general file stream and has capabilities of both.

Creating class object

We are taking a class Employee with Name, Employee_ID, and Salary as its public data members.

class Employee {
   public:
      char Name[];
      long Employee_ID;
      int Salary;
} ;
Employee Emp_1;
Emp_1.Name=”Jhonson”;
Emp_1.Employee_ID=212020;
Emp_1.Salary=11000;

Creating file objects

Syntax

fstream/ofstream/ifstream object_name;
void open(const char *filename, ios::openmode);
ofstream file1;
file1.open( “Employee.txt”, ios::app );
  • Here file1 is the object used to open the file Employee.txt in append mode (new content gets appended at the end). Type of file1 object is ofstream which means we can write into Employee.txt.

ifstream file2;
file2.open( “Employee.txt”, ios::in );
  • Here file2 is the object used to open the file Employee.txt in input mode to read the contents. Type of file1 object is ifstream which means we can only read data from Employee.txt.

Writing and Reading class objects

file1.write( (char*)&Emp_1, sizeof(Emp1) );
  • Here data present in class object Emp_1 is written to file Employee.txt by calling write function. (char*)&Emp_1 is used to point at the start of an object and sizeof(Emp_1) calculates the number of bytes copied in file.

file2.read( (char*)&Emp_1, sizeof(Emp1) );
  • Here data present in class object Emp_1 is read from file Employee.txt by calling read function. (char*)&Emp_1 is used to point at the start of an object and sizeof(Emp_1) calculates the number of bytes read from the file.

Closing the file

file1.close();
file2.close();

To close the input stream and output stream of files.

Example

 Live Demo

#include <iostream>
#include <fstream>
using namespace std;
// Class to define the properties
class Employee {
public:
   string Name;
   int Employee_ID;
   int Salary;
};
int main(){
   Employee Emp_1;
   Emp_1.Name="John";
   Emp_1.Employee_ID=2121;
   Emp_1.Salary=11000;
   //Wriring this data to Employee.txt
   ofstream file1;
   file1.open("Employee.txt", ios::app);
   file1.write((char*)&Emp_1,sizeof(Emp_1));
   file1.close();
   //Reading data from EMployee.txt
   ifstream file2;
   file2.open("Employee.txt",ios::in);
   file2.seekg(0);
   file2.read((char*)&Emp_1,sizeof(Emp_1));
   printf("\nName :%s",Emp_1.Name);
   printf("\nEmployee ID :%d",Emp_1.Employee_ID);
   printf("\nSalary :%d",Emp_1.Salary);
   file2.close();
   return 0;
}

Output

Name: John
Employee ID: 2121
Salary: 11000
raja
Published on 28-Jul-2020 14:05:13
Advertisements