#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<process.h>
#include<conio.h>
class data
{
int id;
char fname[20],lname[20],pno[14];
public:
void add();
void display();
int getId(){ return id;}
char* getfname()
{
return fname;
}
}d;
void storedata();
void readdata();
void searchdata();
void deldata();
void main()
{
char n;
clrscr();
do
{
cout<<endl<<endl<<endl<<"\t\t\t Main Menu";
cout<<endl<<"\t\t\t============================";
cout<<endl<<"\t\t\t 0. EXIT";
cout<<endl<<"\t\t\t 1. Add Data";
cout<<endl<<"\t\t\t 2. Display Data";
cout<<endl<<"\t\t\t 3. Search Data";
cout<<endl<<"\t\t\t 4. Delete Data";
cout<<endl<<"\t\t\t CHOOSE : ";
n=getche();
switch(n)
{
case '0': exit(0); break;
case '1':
clrscr();
storedata();
clrscr();
textcolor(RED+BLINK);
cprintf("!Data Added");
textcolor(7);
break;
case '2':
clrscr();
readdata();
getch();
clrscr();
break;
case '3':
clrscr();
searchdata();
break;
case '4':
clrscr();
deldata();
break;
default :
clrscr();
textcolor(RED+BLINK);
cprintf("Invalid Input");
textcolor(7);
}
}
while('n'!='0');
}
void data::add()
{
cout<<"Enter Your Id : ";
cin>>id;
cout<<"Enter Your First Name : ";
cin.get();
cin.getline(fname,20);
cout<<"Enter Your Last Name : ";
cin.getline(lname,20);
cout<<"Enter Your Phone No : ";
cin.getline(pno,14);
}
void data::display()
{
cout<<"Id : "<<id;
cout<<endl<<"Name : "<<fname<<" "<<lname;
cout<<endl<<"Phone No : "<<pno;
}
void storedata()
{
ofstream fout;
fout.open("data.dat",ios::binary|ios::app);
d.add();
fout.write((char*)&d,sizeof(d));
fout.close();
}
void readdata()
{
ifstream fin;
fin.open("data.dat");
while(fin.read((char*)&d,sizeof(d)))
{
d.display();
cout<<endl<<endl;
}
fin.close();
}
void searchdata()
{
int fid;
cout<<"Enter Id : ";
cin>>fid;
ifstream fin;
fin.open("data.dat");
while(fin.read((char*)&d,sizeof(d)))
{
if(fid==d.getId())
{
d.display();
cout<<endl<<endl;
}
}
fin.close();
}
void deldata()
{
char ffname[20];
ofstream fout;
cout<<"Enter First Name : ";
cin.getline(ffname,20);
ifstream fin;
fin.open("data.dat");
fout.open("data1.dat");
while(fin.read((char*)&d,sizeof(d)))
{
if(stricmp(ffname,d.getfname())!=0)
{
fout.write((char *)&d,sizeof(d));
}
}
fout.close();
fin.close();
remove("data.dat");
rename("data1.dat","data.dat");
fin.close();
}
No comments:
Post a Comment