forked from Priyadarshan2000/Hacktoberfest_2k22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact-management-system.c
223 lines (210 loc) Β· 4.4 KB
/
contact-management-system.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
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdlib.h>
#include<dos.h>
#include<ctype.h>
struct contact
{
long ph;
char name[100],address[100],email[100];
} list;
char query[20],name[20];
FILE *fp, *ft;
int i,n,ch,l,found;
//add contact
void AddContact()
{
fp=fopen("contact.dat","a");
for (;;)
{
fflush(stdin);
printf("To exit enter blank space in the name input\nEnter the Name:");
scanf("%[^\n]",&list.name);//searches for string until line break or enter
if(stricmp(list.name,"")==0 || stricmp(list.name," ")==0)
{
break;
}
fflush(stdin);
printf("Enter the Phone Number: ");
scanf("%ld",&list.ph);
fflush(stdin);
printf("Enter the Address: ");
fflush(stdin);
gets(list.address);
printf("Enter the Email-id: ");
fflush(stdin);
gets(list.email);
printf("\n");
fwrite(&list,sizeof(list),1,fp);
printf("Successfully saved the Contact. \n");
}
fclose(fp);
}
//list all contacts
void ListContacts()
{
system("cls");
printf("\n\t\t\n\t\t\tLIST OF CONTACTS\n\t\t");
for(i=97; i<=122; i=i+1)
{
fp=fopen("contact.dat","r");
fflush(stdin);
found=0;
while(fread(&list,sizeof(list),1,fp)==1)
{
if(list.name[0]==i || list.name[0]==i-32)
{
printf("\nName\t: %s\nPhone\t: %ld\nAddress\t: %s\nEmail\t: %s\n",list.name,list.ph,list.address,list.email);
found++;
}
}
if(found!=0)
{
printf("[%c]-(%d)\n\n",i-32,found);
getch();
}
fclose(fp);
}
}
//search contact
void searchContact()
{
system("cls");
printf("\n\t\t\n\t\t\tSEARCH CONTACT\n\t\t");
fflush(stdin);
printf("Enter the name of the contact to be searched: ");
scanf("%[^\n]",&query);
fp=fopen("contact.dat","r");
fflush(stdin);
found=0;
while(fread(&list,sizeof(list),1,fp)==1)
{
if(stricmp(list.name,query)==0)
{
printf("\nName\t: %s\nPhone\t: %ld\nAddress\t: %s\nEmail\t: %s\n",list.name,list.ph,list.address,list.email);
found++;
}
}
if(found==0)
{
printf("\n\nContact not found.\n");
}
fclose(fp);
getch();
}
//edit contact
void editContact()
{
system("cls");
fp=fopen("contact.dat","r");
ft=fopen("temp.dat","w");
fflush(stdin);
printf("Edit contact\n\n\n\tEnter the name of contact to edit:");
scanf("%[^\n]",name);
while(fread(&list,sizeof(list),1,fp)==1)
{
if(stricmp(name,list.name)!=0)
fwrite(&list,sizeof(list),1,ft);
}
fflush(stdin);
printf("\n\nEditing '%s'\n\n",name);
printf("Name: ");
scanf("%[^\n]",&list.name);
fflush(stdin);
printf("Phone: ");
scanf("%ld",&list.ph);
fflush(stdin);
printf("Address: ");
scanf("%[^\n]",&list.address);
fflush(stdin);
printf("Email-id: ");
gets(list.email);
printf("\n");
fwrite(&list,sizeof(list),1,ft);
fclose(fp);
fclose(ft);
remove("contact.dat");
rename("temp.dat","contact.dat");
}
//delete contact
void deleteContact()
{
system("cls");
fp=fopen("contact.dat","r");
ft=fopen("temp.dat","w");
fflush(stdin);
printf("\n\n\tEnter the name of contact to delete:");
scanf("%[^\n]",name);
while(fread(&list,sizeof(list),1,fp)==1)
{
if(stricmp(name,list.name)!=0)
fwrite(&list,sizeof(list),1,ft);
}
fclose(fp);
fclose(ft);
remove("contact.dat");
rename("temp.dat","contact.dat");
}
int showMenu()
{
int choice = 0;
printf("\n");
printf("\t\t\tContact Management System\n");
printf("\n");
printf("\t\t\t\tMAIN MENU\n\n\t\t\t[1] Add a new Contact\n\t\t\t[2] List all Contacts\n\t\t\t[3] Search for contact\n\t\t\t[4] Edit a Contact\n\t\t\t[5] Delete a Contact\n\t\t\t[0] Exit\n");
printf("\n");
printf("\t\t\tEnter the choice:");
scanf("%d",&choice);
return choice;
}
int main()
{
system("cls");
int ch = 0;
while (1)
{
ch = showMenu();
switch(ch)
{
case 0:
{
printf("\n");
printf("Logging Out from the Contact Management System.\n");
exit(0);
}
case 1:
{
AddContact();
break;
}
case 2:
{
ListContacts();
break;
}
case 3:
{
searchContact();
break;
}
case 4:
{
editContact();
break;
}
case 5:
{
deleteContact();
break;
}
default:
{
printf("\n");
printf("Invalid Input\n");
break;
}
}
}
}