-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_TELEPHONE_FILE.cpp
242 lines (198 loc) · 4.55 KB
/
2_TELEPHONE_FILE.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include<iostream>
#include<conio.h>
#include<string.h>
#include<fstream>//imp
using namespace std;
class info
{
private:
char name[20];
char telephone_no[10];
public:
friend class directory;
};
class directory
{
private:
char file_name[10];
info ob; //IMP
public:
//------------------------------------------------------------------------------
directory()
{
cout<<"Enter file name : ";
cin>>file_name;
}
//-------------------------------------------------------------------------------
void create_file()
{
ofstream f;
f.open(file_name,ios::out|ios::app|ios::binary);
//if binary not specified it will be a text file
if(!f)
{
cout<<"\n***error opening the file***";
return;
}
cout<<"\n***enter name & to end file***\n\n";
while(1)
{
cout<<"NAME : ";
cin>>ob.name;
if(strcmp(ob.name,"&")==0)
break;
cout<<"TELEPHONE NO. : ";
cin>>ob.telephone_no;
f.write((char*)&ob,sizeof(ob)); //Find Alternative
cout<<"\n";
}
f.close();
}
//-----------------------------------------------------------------------------
void display_file()
{
ifstream f;
f.open(file_name,ios::in|ios::binary);
if(!f)
{
cout<<"\n***error opening the file***";
return;
}
char search_name[20];
f.read((char*)&ob,sizeof(ob));
while(!f.eof())
{
cout<<"NAME : "<<ob.name<<" TELEPHONE NO : "<<ob.telephone_no<<endl;
f.read((char*)&ob,sizeof(ob));
}
f.close();
}
//------------------------------------------------------------------------------
void search_by_name()
{
char search_name[20];
ifstream f;
f.open(file_name,ios::in|ios::binary);
if(!f)
{
cout<<"\n***error opening the file***";
return;
}
cout<<"enter the name to be search : ";
cin>>search_name;
f.read((char*)&ob,sizeof(ob));
while(!f.eof())
{
if(strcmp(search_name,ob.name)==0)
{
cout<<"NAME : "<<ob.name<<" TELEPHONE NO. : "<<ob.telephone_no<<"\n";
f.close();
return;
}
f.read((char*)&ob,sizeof(ob));
}
cout<<"Name not found"<<endl;
f.close();
}
//-------------------------------------------------------------------------------
void search_by_telephone_no()
{
char search_telephone[20];
ifstream f;
f.open(file_name,ios::in|ios::binary);
if(!f)
{
cout<<"\n***error opening the file***";
return;
}
cout<<"enter the telephone no. to be search : ";
cin>>search_telephone;
f.read((char*)&ob,sizeof(ob));
while(!f.eof())
{
if(strcmp(search_telephone,ob.telephone_no)==0)
{
cout<<"NAME : "<<ob.name<<" TELEPHONE NO. : "<<ob.telephone_no<<"\n";
f.close();
return;
}
f.read((char*)&ob,sizeof(ob));
}
cout<<"telephone no. not found"<<endl;
f.close();
}
//-------------------------------------------------------------------------------
void update_telephone_no()
{
char search_name[20];
char new_tell_no[20];
fstream f;
f.open(file_name,ios::in);
ofstream fo;
fo.open("temp.txt",ios::out);
if(!f)
{
cout<<"Error opening the file"<<endl;
return;
}
cout<<"enter name to search : ";
cin>>search_name;
f.read((char*)&ob,sizeof(ob));
while(!f.eof())
{
if(strcmp(search_name,ob.name)==0)
{
cout<<"enter new telephone no : ";
cin>>ob.telephone_no;
fo.write((char*)&ob,sizeof(ob));
}
else
{
fo.write((char*)&ob,sizeof(ob));
}
f.read((char*)&ob,sizeof(ob));
}
f.close();
fo.close();
remove(file_name);
rename("temp.txt",file_name);
}
//-------------------------------------------------------------------------------
};
main()
{
directory d1;
int op=-1;
while(op!=0)
{
cout<<"\n1.CREATE FILE";
cout<<"\n2.DISPLAY";
cout<<"\n3.SEARCH BY NAME";
cout<<"\n4.SEARCH BY TELEPHONE NO.";
cout<<"\n5.UPDATE TELEPHONE NO.";
cout<<"\n6.EXIT";
cout<<"\n\nenter choice : ";
cin>>op;
switch(op)
{
case 1:
d1.create_file();
break;
case 2:
d1.display_file();
break;
case 3:
d1.search_by_name();
break;
case 4:
d1.search_by_telephone_no();
break;
case 5:
d1.update_telephone_no();
break;
case 6:
op=0;
break;
}
}
}