-
Notifications
You must be signed in to change notification settings - Fork 2
/
phone_directory.cpp
162 lines (149 loc) · 5.45 KB
/
phone_directory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//Telephone Directory
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
using namespace std;
class phoneBook{
char name[20],phno[15];
public:
void getdata();
void showdata();
char *getname(){ return name; }
char *getphno(){ return phno; }
void update(char *nm,char *telno){
strcpy(name,nm);
strcpy(phno,telno);
}
};
void phoneBook :: getdata(){
cout<<"\nEnter New record Details";
cout<<"\nEnter New Name : ";
cin>>name;
cout<<"Enter New Telephone Number : ";
cin>>phno;
}
void phoneBook :: showdata(){
cout<<"\n";
cout<<setw(20)<<name;
cout<<setw(15)<<phno;
}
int main(){
phoneBook rec;
int flag;
fstream file;
char ch,nm[20],telno[15];
int choice,found=0;
while(1){
cout<<"\n\t*****PHONE BOOK*****\n";
cout<<"1) Add New Record\n";
cout<<"2) Display All Records\n";
cout<<"3) Search Telephone Number\n";
cout<<"4) Search Person's Name\n";
cout<<"5) Update Telephone Number\n";
cout<<"6) Delete Telephone Number\n";
cout<<"7) Exit\n\n";
cout<<"Select your choice : ";
cin>>choice;
switch(choice){
case 1 : //New Record
file.open("/home/dheeraj/Desktop/c++project/phonebk.dat", ios::out | ios::app|ios::binary);
rec.getdata();
rec.showdata();
file.write((char *) &rec, sizeof(rec));
file.close();
break;
case 2 : //Display All Records
file.open("/home/dheeraj/Desktop/c++project/phonebk.dat", ios::in|ios::binary);
char ch,nm[20],telno[6];
file.seekg(0,ios::beg);
cout<<"\n\nRecords in Phone Book\n\n";
while(file.read((char *) &rec, sizeof(rec))){
rec.showdata();
}
file.close();
break;
case 3 : //Search Tel. no. when person's name is known.
file.open("/home/dheeraj/Desktop/c++project/phonebk.dat", ios::in|ios::binary);
cout<<"\n\nEnter Name of the Person : ";
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.showdata();
}
}
file.clear();
if(found==0)
cout<<"\n\tWARNING: Record Not found \n\n";
getchar();
file.close();
break;
case 4 : //Search name on basis of tel. no
file.open("/home/dheeraj/Desktop/c++project/phonebk.dat", ios::in|ios::binary);
cout<<"\n\nEnter Telephone Number of the person : ";
cin>>telno;
file.seekg(0,ios::beg);
found=0;
while(file.read((char *) &rec, sizeof(rec))){
if(strcmp(telno,rec.getphno())==0){
found=1;
rec.showdata();
}
}
file.clear();
if(found==0)
cout<<"\n\tWARNING: Record Not found \n\n";
getchar();
file.close();
break;
case 5 : //Update Telephone No.
file.open("/home/dheeraj/Desktop/c++project/phonebk.dat", ios::in|ios::out|ios::binary|ios::ate);
cout<<"\n\nEnter Name : ";
cin>>nm;
file.seekg(0);
while(file.read((char *) &rec, sizeof(rec)))
{
if(strcmp(nm,rec.getname())==0)
{
cout<<"Enter New Telephone Number"<<endl;
cin>>telno;
file.seekp(-sizeof(rec),ios::cur);
rec.update(nm,telno);
file.write((char *) &rec, sizeof(rec));
}
}
file.clear();
file.close();
break;
case 6 :{//Delete
fstream temp1;
temp1.open("/home/dheeraj/Desktop/c++project/tempbk.dat", ios::out|ios::binary|ios::app);
file.open("/home/dheeraj/Desktop/c++project/phonebk.dat", ios::in|ios::binary);
cout<<"\n\nEnter Name You want to Delete : ";
cin>>nm;
file.seekg(0);
while(1){
flag=0;
file.read((char *) &rec, sizeof(rec));
if(file.eof()) break;
if(strcmp(nm,rec.getname())==0){
flag++;
}
if(!flag)
temp1.write((char *) &rec, sizeof(rec));
}
file.close();
temp1.close();
remove("/home/dheeraj/Desktop/c++project/phonebk.dat");
rename("/home/dheeraj/Desktop/c++project/tempbk.dat","/home/dheeraj/Desktop/c++project/phonebk.dat");
break;}
case 7 : //file.close();
exit (0);
default : std::cout << "\n\t wrong choice !" << '\n';
}
}
return 0;
}