Sunday, February 27, 2011

File Handling reading and writing in C++

#include<fstream.h>
#include<conio.h>
 class employee
 {
 int emp_id;
 char emp_na[40];
 public:
 void get_data()
 {
 cout<<"\n enter Emp Id:";
 cin>>emp_id;
 cout<<"\n enter Emp Name:";
 cin>>emp_na;
 }

void show_data()
 {
 cout<<"\n Employee Id:"<<emp_id;
 cout<<"\n Employee Name:"<<emp_na;
 }
 };

void main()
 {
 clrscr();
 employee e1;
 ofstream fp;
 ifstream fp1;
 fp.open("emp.txt",ios::app);
 e1.get_data();
 fp.write((char*)&e1,sizeof(e1));
 fp.close();
 getch();

fp1.open("emp.txt",ios::in);
 fp1.read((char*)&e1,sizeof(e1));
 while(!fp1.eof())
 {
 e1.show_data();
 fp1.read((char*)&e1,sizeof(e1));
 }
 fp.close();
 getch();
 }

No comments:

Post a Comment