#include<fstream.h>
#include<conio.h>
#include<ctype.h>
#include<iomanip.h>
#include<stdio.h>
//#include<dos.h>
char ans;
int a=0;
class subscriber
{
char nm[30];
char adrs[30];
long int phone;
public:
void enter_rec();
void disp_rec();
int check(int);
void disp_rec_inline(int);
long int inp_phone()
{
long int x;
x=phone;
return x;
}
};
int subscriber::check(int p)
{
subscriber s;
ifstream in("TD1.DAT",ios::binary);
while(in.read((char *) & s,sizeof(subscriber)))
{
if(s.phone==p)
{
in.close();
return 0;
}
if(in.eof())
break;
}
in.close();
return 1;
}
void subscriber::enter_rec()
{
subscriber s;
again: clrscr();
gotoxy(32,8);
cout<<"HELLO SUBSCRIBER";
gotoxy(30,10);
cout<<"Enter data-";
gotoxy(30,12);
cout<<"Enter name :";
// gets(nm);
cin>>nm;
gotoxy(30,13);
cout<<"Enter address :";
// gets(adrs);
cin>>adrs;
gotoxy(30,14);
cout<<"Enter phone no.:";
cin>>phone;
int f= check(phone);
if(f==0)
{
gotoxy(30,17);
cout<<"ALREADY EXISTS...........";
getch();
goto again;
}
}
void subscriber::disp_rec()
{
gotoxy(30,10);
cout<<"Subscriber details-";
gotoxy(30,12);
cout<<"Name :";puts(nm);
gotoxy(30,13);
cout<<"Address :";puts(adrs);
gotoxy(30,14);
cout<<"Phone no.:"<<phone;
}
void subscriber::disp_rec_inline(int ln)
{
cout<<endl;
cout.setf(ios::right, ios::adjustfield);
cout << setw(6) << ln << " ";
cout.setf(ios::left, ios::adjustfield);
cout<<setw(25)<<nm<<" ";
cout.setf(ios::left, ios::adjustfield);
cout<<setw(30)<<adrs;
cout<<setw(12)<<phone;
}
void inp_data()
{
subscriber s;
ofstream f1("TD1.DAT",ios::binary|ios::app);
do
{
s.enter_rec();
f1.write((char *) & s,sizeof(subscriber));
gotoxy(30,16);
cout<<"Any more data?";
ans=toupper(getche());
}while(ans!='N');
f1.close();
}
void show_hdr()
{
clrscr();
gotoxy(30, 2);cout << "SUBSCRIBER LIST";
cout <<endl<<endl<<" SUBS.";
cout.setf(ios::left, ios::adjustfield);
cout<< setw(27) << " SUBSCRIBER'S NAME";
cout.setf(ios::left, ios::adjustfield);
cout << setw(4)<< " ADDRESS";
cout.setf(ios::right, ios::adjustfield);
cout << setw(32) << " PHONE NO."<<endl;
}
void disp_data()
{
subscriber s;
ifstream f2("TD1.DAT",ios::binary|ios::in);
int ln_cnt=0;
do
{
f2.read((char *) &s,sizeof(subscriber));
if(!f2.eof())
{
if (ln_cnt==0)
show_hdr();
else
{ if (ln_cnt%19==0)
{
cout << "More....";getche(); show_hdr();
ln_cnt=0;
}
}
ln_cnt++; s.disp_rec_inline(ln_cnt);
}
} while(!f2.eof());
f2.close();
getche();
}
void search()
{
subscriber s;
int flag=0;
long int s_phone;
gotoxy(15,12);
cout<<"Enter phone no. of the subscriber to be searched:";cin>>s_phone;
ifstream f3("TD1.DAT",ios::binary|ios::app);
do
{
f3.read((char *) & s,sizeof(subscriber));
if(s.inp_phone()==s_phone)
{
if(!f3.eof())
{
clrscr();
s.disp_rec();
flag=1; break;
}
}
}while(!f3.eof());
if(flag==0)
{
clrscr();
gotoxy(30,12);
cout<<"No such subscriber!!!";
}
f3.close();
getch();
}
void dele_record()
{
subscriber s;
int flag=0;
long int s_phone;
gotoxy(15,12);
cout<<"Enter phone no. of the subscriber to be deleted:";cin>>s_phone;
ifstream f3("TD1.DAT",ios::binary|ios::in);
ofstream f4("TMTD1.DAT",ios::binary);
do
{
f3.read((char *) & s,sizeof(subscriber));
if (!f3.eof())
{ if(s.inp_phone()==s_phone)
{
clrscr();
gotoxy(23,8);
cout<<"The subscriber being deleted is:\n";
s.disp_rec();
flag=1; break;
}
else
f4.write((char *) & s,sizeof(subscriber));
}
}while(!f3.eof());
if(flag==0)
{
clrscr();
gotoxy(30,12);
cout<<"No such subscriber!!!";
}
while(!f3.eof())
{
f3.read((char *) & s,sizeof(subscriber));
if (!f3.eof())
f4.write((char *) & s,sizeof(subscriber));
}
f3.close(); f4.close();
remove("TD1.DAT");
rename("TMTD1.DAT", "TD1.DAT");
getch();
}
void mod_data()
{
int flag=0,recno;
long int s_phone;
subscriber s;
gotoxy(15,12);
cout<<"Enter phone no. of the subscriber to be modified:";cin>>s_phone;
ifstream fl("TD1.DAT",ios::binary|ios::in);
ofstream fz("TMTD1.DAT",ios::binary);
do
{
fl.read((char *) & s,sizeof(subscriber));
if (!fl.eof())
{ if(s.inp_phone()==s_phone)
{
clrscr();
s.disp_rec();
flag=1; gotoxy(30,17);
cout<<"Press any key!!";
break;
}
else
fz.write((char *) & s,sizeof(subscriber));
}
}while(!fl.eof());
if(flag==0)
{
clrscr();
gotoxy(30,12);
cout<<"No such subscriber!!!";
}
while(!fl.eof())
{
fl.read((char *) & s,sizeof(subscriber));
if (!fl.eof())
fz.write((char *) & s,sizeof(subscriber));
}
fl.close(); fz.close();
remove("TD1.DAT");
rename("TMTD1.DAT", "TD1.DAT");
getch();
ifstream f3("TD1.DAT",ios::binary|ios::app);
do
{
f3.read((char *) & s,sizeof(subscriber));
if(s.inp_phone()==s_phone)
{
if(!f3.eof())
{
s.disp_rec();
flag=1; break;
}
}
}while(!f3.eof());
if(flag==0)
{
clrscr();
gotoxy(30,12);
cout<<"No such record!!!";
}
recno=f3.tellg();
f3.close();
a=(recno-(1*sizeof(subscriber)));
fstream f4("TD1.DAT",ios::binary|ios::in|ios::out|ios::app);
f4.seekp(a,ios::beg);
s.enter_rec();
f4.write((char *) & s,sizeof(subscriber));
f4.close();
}
void main()
{
int opt,r,d,x,c;
long int s_phone;
subscriber s;
textcolor(CYAN);
clrscr();
_setcursortype(_NOCURSOR);
// starting();
do
{
clrscr();
r=4;d=18; //for adjusting top & bottom lines
for(x=5;x<=75;x=x+2)
{
gotoxy(x,r);cout<<"*";
gotoxy(x,d);cout<<"*";
}
c=5;d=75; //for adjusting side lines
for(x=4;x<=18;x++)
{
gotoxy(c,x);cout<<"*";
gotoxy(d,x);cout<<"*";
}
gotoxy(31,3); cout<<"TELEPHONE DIRECTORY";
gotoxy(30,6); cout<<"1.Add subscriber";
gotoxy(30,8); cout<<"2.Search subscriber";
gotoxy(30,10); cout<<"3.Modify subscriber";
gotoxy(30,12); cout<<"4.Delete subscriber";
gotoxy(30,14); cout<<"5.Display subscribers";
gotoxy(30,16); cout<<"6.Exit";
gotoxy(1,21); cout<<"Enter your choice:";
cin>>opt;
switch(opt)
{
case 1:clrscr();
inp_data();
break;
case 2:clrscr();
search();
break;
case 3:clrscr();
mod_data();
break;
case 4:clrscr();
dele_record();
break;
case 5:clrscr();
disp_data();
break;
case 6:break;
default:cout<<"\nWrong choice !!!";
getch();
}
}while(opt!=6);
}