-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathPhonebook.c
332 lines (290 loc) · 6.63 KB
/
Phonebook.c
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//Phonebook in C by Akshat Pandey
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
struct person
{
char name[35];
char address[50];
char father_name[35];
char mother_name[30];
long int mble_no;
char sex[8];
char mail[100];
char citision_no[20];
};
void menu();
void got();
void start();
void back();
void addrecord();
void listrecord();
void modifyrecord();
void deleterecord();
void searchrecord();
int main()
{
system("color 5f");
start();
return 0;
}
void back()
{
start();
}
void start()
{
menu();
}
void menu()
{
system("cls");
printf("\t\t**********WELCOME TO PHONEBOOK*************");
printf("\n\n\t\t\t MENU\t\t\n\n");
printf("\t1.Add New \t2.List \t3.Exit \n\t4.Modify \t5.Search\t6.Delete");
switch(getch())
{
case '1':
addrecord();
break;
case '2': listrecord();
break;
case '3': exit(0);
break;
case '4': modifyrecord();
break;
case '5': searchrecord();
break;
case '6': deleterecord();
break;
default:
system("cls");
printf("\nEnter 1 to 6 only");
printf("\n Enter any key");
getch();
menu();
}
}
void addrecord()
{
system("cls");
FILE *f;
struct person p;
f=fopen("project","ab+");
printf("\n Enter name: ");
got(p.name);
printf("\nEnter the address: ");
got(p.address);
printf("\nEnter father name: ");
got(p.father_name);
printf("\nEnter mother name: ");
got(p.mother_name);
printf("\nEnter phone no.:");
scanf("%ld",&p.mble_no);
printf("Enter sex:");
got(p.sex);
printf("\nEnter e-mail:");
got(p.mail);
printf("\nEnter citizen no:");
got(p.citision_no);
fwrite(&p,sizeof(p),1,f);
fflush(stdin);
printf("\nrecord saved");
fclose(f);
printf("\n\nEnter any key");
getch();
system("cls");
menu();
}
void listrecord()
{
struct person p;
FILE *f;
f=fopen("project","rb");
if(f==NULL)
{
printf("\nfile opening error in listing :");
exit(1);
}
while(fread(&p,sizeof(p),1,f)==1)
{
printf("\n\n\n YOUR RECORD IS\n\n ");
printf("\nName=%s\nAdress=%s\nFather name=%s\nMother name=%s\nMobile no=%ld\nSex=%s\nE-mail=%s\nCitizen no=%s",p.name,p.address,p.father_name,p.mother_name,p.mble_no,p.sex,p.mail,p.citision_no);
getch();
system("cls");
}
fclose(f);
printf("\n Enter any key");
getch();
system("cls");
menu();
}
void searchrecord()
{
struct person p;
FILE *f;
char name[100];
f=fopen("project","rb");
if(f==NULL)
{
printf("\n error in opening\a\a\a\a");
exit(1);
}
printf("\nEnter name of person to search\n");
got(name);
while(fread(&p,sizeof(p),1,f)==1)
{
if(strcmp(p.name,name)==0)
{
printf("\n\tDetail Information About %s",name);
printf("\nName:%s\naddress:%s\nFather name:%s\nMother name:%s\nMobile no:%ld\nsex:%s\nE-mail:%s\nCitision no:%s",p.name,p.address,p.father_name,p.mother_name,p.mble_no,p.sex,p.mail,p.citision_no);
}
else
printf("file not found");
}
fclose(f);
printf("\n Enter any key");
getch();
system("cls");
menu();
}
void deleterecord()
{
struct person p;
FILE *f,*ft;
int flag;
char name[100];
f=fopen("project","rb");
if(f==NULL)
{
printf("CONTACT'S DATA NOT ADDED YET.");
}
else
{
ft=fopen("temp","wb+");
if(ft==NULL)
printf("file opaning error");
else
{
printf("Enter CONTACT'S NAME:");
got(name);
fflush(stdin);
while(fread(&p,sizeof(p),1,f)==1)
{
if(strcmp(p.name,name)!=0)
fwrite(&p,sizeof(p),1,ft);
if(strcmp(p.name,name)==0)
flag=1;
}
fclose(f);
fclose(ft);
if(flag!=1)
{
printf("NO CONACT'S RECORD TO DELETE.");
remove("temp.txt");
}
else
{
remove("project");
rename("temp.txt","project");
printf("RECORD DELETED SUCCESSFULLY.");
}
}
}
printf("\n Enter any key");
getch();
system("cls");
menu();
}
void modifyrecord()
{
int c;
FILE *f;
int flag=0;
struct person p,s;
char name[50];
f=fopen("project","rb+");
if(f==NULL)
{
printf("CONTACT'S DATA NOT ADDED YET.");
exit(1);
}
else
{
system("cls");
printf("\nEnter CONTACT'S NAME TO MODIFY:\n");
got(name);
while(fread(&p,sizeof(p),1,f)==1)
{
if(strcmp(name,p.name)==0)
{
printf("\n Enter name:");
got(s.name);
printf("\nEnter the address:");
got(s.address);
printf("\nEnter father name:");
got(s.father_name);
printf("\nEnter mother name:");
got(s.mother_name);
printf("\nEnter phone no:");
scanf("%ld",&s.mble_no);
printf("\nEnter sex:");
got(s.sex);
printf("\nEnter e-mail:");
got(s.mail);
printf("\nEnter citizen no\n");
got(s.citision_no);
fseek(f,-sizeof(p),SEEK_CUR);
fwrite(&s,sizeof(p),1,f);
flag=1;
break;
}
fflush(stdin);
}
if(flag==1)
{
printf("\n your data id modified");
}
else
{
printf(" \n data is not found");
}
fclose(f);
}
printf("\n Enter any key");
getch();
system("cls");
menu();
}
void got(char *name)
{
int i=0,j;
char c,ch;
do
{
c=getch();
if(c!=8&&c!=13)
{
*(name+i)=c;
putch(c);
i++;
}
if(c==8)
{
if(i>0)
{
i--;
}
// printf("h");
system("cls");
for(j=0;j<i;j++)
{
ch=*(name+j);
putch(ch);
}
}
}while(c!=13);
*(name+i)='\0';
}