Saturday, June 22, 2019

FILE HANDLING || TELIPHONE DIERY || C++

CODE :


#include
#include
#include
#include
#include
using namespace std;
class phonebook
{
char name[20],phno[15];
public:
void getdata()
{
cout<<"\n enter name : ";
cin>>name;
cout<<"\n enter number : ";
cin>>phno;
}
void display()
{
cout<
cout<
cout<
}
char *getname()
{
return name;
}
char *getphno()
{
return phno;
}
void update(char *nm,char *telno)
{
strcpy(name,nm);
strcpy(phno,telno);
}
};
int main()
{
phonebook rec;
fstream file;
file.open("/home/student/Desktopphone.data",ios::ate | ios::in | ios::binary);
char ch,nm[30],telno[14];
int choice,found=0;
int cnt=0;
while(1)
{
cout<<"\n\n\n\n********** #Phone Book# ***********\n";
cout<<"1. Add New Record \n";
cout<<"2. Display All Record \n";
cout<<"3. Search Telephone No. \n";
cout<<"4. Search Person name \n";
cout<<"5. Update Telephone No. \n";
cout<<"6. Exit \n";
cout<<"Enter choice : ";
cin>>choice;
switch(choice)
{
case 1:
rec.getdata();
cin.get(ch);
file.write((char *) &rec, sizeof(rec));
break;
case 2:
file.seekg(0,ios::beg);
cout<<"\n\n REcords in Phone Book \n";
while(file)
{
file.read((char *) &rec, sizeof(rec));
if(!file.eof())
rec.display();
}
file.clear();
getchar();
break;
case 3:
cout<<"\n\n Enter Name : ";
cin>>nm;
file.seekg(0,ios::beg);
found=0;
while(file.read((char *) &rec, sizeof(rec)))
{
if(strcmp(nm,rec.getname())==0)
{
found=1;
rec.display();
}
}
file.clear();
if(found==0)
cout<<"\n\n -----Record Not Found----- \n";
getchar();
break;
case 4:
cout<<"\n\n Enter Telephone No. : ";
cin>telno;
file.seekg(0,ios::beg);
found=0;
while(file.read((char *) &rec, sizeof(rec)))
{
if(strcmp(nm,rec.getphno())==0)
{
found=1;
rec.display();
}
}
file.clear();
if(found==0)
cout<<"\n\n -----Record Not Found----- \n";
getchar();
break;
case 5:
cout<<"\n\n Enter Name : ";
cin>>nm;
file.seekg(0,ios::beg);
found=0;
cnt=0;
while(file.read((char *) &rec, sizeof(rec)))
{
cnt++;
if(strcmp(nm,rec.getname())==0)
{
found=1;
break;
}
}
file.clear();
if(found==0)
cout<<"\n\n ------Record Not Found----- \n";
else
{
int location = (cnt-1) * sizeof(rec);
cin.get(ch);
if(file.eof())
file.clear();
cout<<"Enter New Telephone No. : ";
cin>>telno;
file.seekp(location);
rec.update(nm,telno);
file.write((char *) &rec, sizeof(rec));
file.flush();
}
break;
case 6:
exit (0);
default:
cerr<<"********** Please Enter Valid Choice **********";
}
}
return 0;

}

OUTPUT :-



No comments:

Post a Comment

STRING OPERATIONS || CPP

STRING OPERATIONS USING CPP                                                                                                             ...