Home » Code Snippets » C/C++ Code Snippets

C++ program to write and read an object in/from a binary file

In this C++ program we will learn how to read an employee's details from keyboard using class and object then write that object into the file? We will also read the object and display employee's record on the screen.

This program is using following file stream (file handling) functions

There will be two functions

  • file_stream_object.open() - to open file
  • file_stream_object.close() - to close the file
  • file_stream_object.write() - to write an object into the file
  • file_stream_object.read() - to read object from the file

In this program there are following details to be read through Employee class

  • Employee ID
  • Employee Name
  • Designation
  • Date of joining
  • Date of birth

Program to write and read an object in, from binary file using write() and read() in C++

#include <iostream>
#include <fstream>
#define FILE_NAME "emp.dat"

using namespace std;

//class employee declaration
class Employee {
private : 
	int 	empID;
	char 	empName[100] ;
	char 	designation[100];
	int 	ddj,mmj,yyj;
	int 	ddb,mmb,yyb;
public  :
	//function to read employee details
	void readEmployee(){
		cout<<"EMPLOYEE DETAILS"<<endl;
		cout<<"ENTER EMPLOYEE ID : " ;
		cin>>empID;
		cin.ignore(1);
		cout<<"ENTER  NAME OF THE EMPLOYEE : ";
		cin.getline(empName,100);

		cout<<"ENTER DESIGNATION : ";
		cin.getline(designation,100);
		
		cout<<"ENTER DATE OF JOIN:"<<endl;
		cout<<"DATE : "; cin>>ddj;
		cout<<"MONTH: "; cin>>mmj;
		cout<<"YEAR : "; cin>>yyj;
		
		cout<<"ENTER DATE OF BIRTH:"<<endl;
		cout<<"DATE : "; cin>>ddb;
		cout<<"MONTH: "; cin>>mmb;
		cout<<"YEAR : "; cin>>yyb;
	}
	//function to write employee details
	void displayEmployee(){
		cout<<"EMPLOYEE ID: "<<empID<<endl
		 <<"EMPLOYEE NAME: "<<empName<<endl
		 <<"DESIGNATION: "<<designation<<endl
		 <<"DATE OF JOIN: "<<ddj<<"/"<<mmj<<"/"<<yyj<<endl
		 <<"DATE OF BIRTH: "<<ddb<<"/"<<mmb<<"/"<<yyb<<endl;
	}
};

int main(){
	
	//object of Employee class
	Employee emp;
	//read employee details
	emp.readEmployee();
	
	//write object into the file
	fstream file;
	file.open(FILE_NAME,ios::out|ios::binary);
	if(!file){
		cout<<"Error in creating file...\n";
		return -1;
	}
	
	file.write((char*)&emp,sizeof(emp));
	file.close();
	cout<<"Date saved into file the file.\n";
	
	//open file again 
	file.open(FILE_NAME,ios::in|ios::binary);
	if(!file){
		cout<<"Error in opening file...\n";
		return -1;
	}
	
	if(file.read((char*)&emp,sizeof(emp))){
			cout<<endl<<endl;
			cout<<"Data extracted from file..\n";
			//print the object
			emp.displayEmployee();
	}
	else{
		cout<<"Error in reading data from file...\n";
		return -1;
	}
	
	file.close();	
	return 0;
}

Output

EMPLOYEE DETAILS
ENTER EMPLOYEE ID : 1001
ENTERNAME OF THE EMPLOYEE : Priya Kaushal 
ENTER DESIGNATION : Student 
ENTER DATE OF JOIN: 
DATE : 21 
MONTH: 11 
YEAR : 2016 
ENTER DATE OF BIRTH:
DATE : 15 
MONTH: 09 
YEAR : 1999 
Date saved into file the file.


Data extracted from file..
EMPLOYEE ID: 1001 
EMPLOYEE NAME: Priya Kaushal
DESIGNATION: Student
DATE OF JOIN: 21/11/2016
DATE OF BIRTH: 15/9/1999


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.