Tuesday, July 10, 2018

Program 8


BINARY FILE HANDLING - II

Write a program to display a particular record to read and modify and thus demonstrate the concept of random access in a data file.


#include<fstream.h>
#include<conio.h>
class employee
{
public:
int empno;
char name[20];
float salary;

void getdata()
{
cout<<"\n enter employee no : ";
cin>>empno;
cout<<"\n enter name : ";
cin>>name;
cout<<"\n enter salary : ";
cin>>salary;
}

void showdata()
{
cout<<empno<<"\t";
cout<<name<<"\t";
cout<<salary<<"\n";
}
};

void main()
{
employee emp;
int num;
fstream f;
f.open("empf.dat",ios::binary|ios::in|ios::out);
f.seekg(0,ios::end);
long end=f.tellg();
cout<<"\n file size : "<<end;
cout<<"\n record size :  \n "<<sizeof(emp);
int n=end/sizeof(emp);
cout<<"\n number of records : "<<n;
cout<<"\n which record do you want to modify ? ";
cin>>num;
int pos=(num-1)*sizeof(emp);
f.seekg(pos);
f.read((char*)&emp,sizeof(emp));
emp.showdata();
emp.getdata();
f.seekg(pos);
f.write((char*)&emp,sizeof(emp));
cout<<"\n record added ";
getch();
}

No comments:

Post a Comment