-
Notifications
You must be signed in to change notification settings - Fork 0
/
phonebook.c
85 lines (81 loc) · 2.37 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
#include <stdio.h> #include <stdlib.h> #include <string.h> void add_item(); void read_item(); void modify_item(); void delete_item(); int option();
void displayheading(); struct phonebook
{
char name[30]; long int pno; char address[30]; };
void main() {
int c;
char con='Y'; while(con=='Y') {
c=option(); switch(c)
{
case 1: add_item(); break;
case 2: read_item(); break;
case 3: modify_item(); break;
case 4:
delete_item();
break;
}
printf("\nType 'Y' to continue with operations:"); scanf("\n%c",&con);
}
}
int option() {
int ch;
printf("\nOptions available:");
printf("\n1 to add contact in phonebook \n2to read a contact
from phonebook \n3 to modifycontacts \n4 to delete a contact from thephonebook");
printf("\nEnter choice: "); scanf("%d",&ch);
return ch;
}
void add_item() {
struct phonebook item; FILE *ft;
char x='Y'; ft=fopen("phonebook","a+"); do
{
char ch;
printf("\nEnter Name: "); scanf("%s",item.name); printf("\nEnter Phone Number: "); scanf("%ld",&item.pno);
printf("\nEnter Email address: "); scanf("%s",item.address);
printf(ft,"%s %ld%s\n",item.name,item.pno,item.address); printf("Type 'Y' to repeat the operation\n"); scanf("\n%c",&ch);
x=ch;
}while(x=='Y');
fclose(ft);
}
void read_item() {
struct phonebook item; FILE *ft; ft=fopen("phonebook","r"); while(feof(ft)==0)
{
fscanf(ft,"%s %ld%s\n",item.name,&item.pno,item.address); printf("\n%s %ld %s",item.name,&item.pno,item.address);
}
fclose(ft);
}
void modify_item()
{
struct phonebook item;
FILE *ft,*fp;
ft=fopen("phonebook","r");
fp=fopen("temp","w+");
long int pno,npno;
printf("\nEnter the phone number which is to be modi ed and
add the new phone number: \n"); scanf("%ld %ld",&pno,&npno); while(!feof(ft))
{
fscanf(ft,"%s %ld %s\n",item.name,&item.pno,item.address); if(item.pno!=pno)
fprintf(fp,"%s %ld %s\n",item.name,item.pno,item.address); else
{
item.pno=npno;
fprintf(fp,"%s %ld %s\n",item.name,item.pno,item.address); }
}
fclose(ft);
fclose(fp);
remove("phonebook");
rename("temp","phonebook"); }
void delete_item() {
struct phonebook item;
FILE *ft,*fp;
long int pno;
ft=fopen("phonebook","r");
fp=fopen("temp","w+");
printf("\nEnter the phone number to be deleted: \n"); scanf("%ld",&pno);
while (!feof(ft))
{
fscanf(ft,"%s %ld %s\n",item.name,&item.pno,item.address); if(pno!=item.pno)
fprintf(fp,"%s %ld %s\n",item.name,item.pno,item.address); }
fclose(ft);
fclose(fp);
remove("phonebook");
rename("temp","phonebook");}s