- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Read/Write Class Objects from/to File in C++
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
#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
- Related Articles
- Write a C program to read a data from file and display
- Read/Write structure to a file using C
- How to read/write data from/to .properties file in Java?
- Read Data from a Text File using C++
- How to read and write a file using Javascript?
- Iseek() in C/C++ to read the alternate nth byte and write it in another file
- How to read contents of a file using Scanner class?
- File Objects in C#
- Read integers from a text file with C++ ifstream
- How to read data from .csv file in Java?
- Write a Python program to read an Excel data from file and read all rows of first and last columns
- Read last line from file in PHP
- How to set read and write position in a file in Python?
- How to open a file in read and write mode with Python?
- How to read data from one file and print to another file in Java?
