Writing Data to Disk ( Files ) Using C++ Fstream Class

Leave a Comment
#include<iostream.h> 
#include<fstream.h>
#include<conio.h>

class emp
//Define Employee Class
{
protected:
char name[30];

int age;
float salary;
public :
void getemp()
{
cout<< "Enter Name:";
cin>>name;
cout<<"Enter Age:";
cin>>age;
cout<<"Enter Salary";
cin>>salary;

}
} ;

void main()
{
emp e;
//declaring object emp of employee class
e.getemp();
//Call getemp() function to input name, age and salary
ofstream outfile("Emp.dat");
//create Emp.dat file
outfile.write((char * )&emp,sizeof(emp));
//write contents of object emp to the file

getche();
}

0 comments: