-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.c
More file actions
251 lines (214 loc) · 5.2 KB
/
Copy pathfunction.c
File metadata and controls
251 lines (214 loc) · 5.2 KB
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
/*
NAME:
DATE:
DESCRIPTION:
SAMPLE I/P:
SAMPLE O/P:
*/
#include"address.h"
int current=0;
void add(contact *con)
{
if(current < MAX_CONTACTS)
{
printf("Enter the contact details:\n");
con[current].index = current+1;
printf("ENTER THE NAME: ");
scanf(" %[^\n]", con[current].name); // Space before %[^\n] is important
printf("ENTER THE PHONE NUMBER: ");
scanf(" %[^\n]", con[current].number);
// Clear input buffer to avoid skipping next input
while (getchar() != '\n');
printf("ENTER THE EMAIL: ");
scanf(" %[^\n]", con[current].mail); // Space before %[^\n] is important
printf("ENTER THE AGE: ");
scanf("%d", &con[current].age);
// Clear input buffer to prepare for the next iteration
while (getchar() != '\n');
printf("ENTER THE ADDRESS: ");
scanf(" %[^\n]", con[current].address);
current++;
}
else
{
printf("ADDRESS BOOK IS FULL\n");
printf("Cannot add more contacts.\n");
}
}
void list(contact *con)
{
if(current == 0)
{
printf("No contacts in the address book.\n");
}
else
{
int j;
printf("\n==================================================================\n");
printf("| ADDRESS BOOK DETAILS |\n");
printf("==================================================================\n");
for (j = 0; j < current ; j++)
{
printf("Index: %d\n", con[j].index);
printf("Name: %s\n", con[j].name);
printf("Phone Number: %s\n", con[j].number);
printf("Email: %s\n", con[j].mail);
printf("Age: %d\n", con[j].age);
printf("Address: %s\n", con[j].address);
printf("==================================================================\n");
}
}
}
void search(contact *con)
{
int i;
char search_name[20];
contact *found = NULL;
printf("enter the name of contact to search:");
scanf("%s",search_name);
for(i=0;i<current;i++)
{
if(strcmp(search_name,con[i].name) == 0)
{
found = &con[i];
break;
}
}
if(found)
{
printf("-----------------------\n");
printf("Index: %d\n", found->index);
printf("Name: %s\n", found->name);
printf("Phone Number: %s\n", found->number);
printf("Email: %s\n", found->mail);
printf("Age: %d\n", found->age);
printf("-----------------------\n");
}
else
{
printf("Contact not found in the list.\n");
}
}
void edit(contact *con)
{
int i;
char search_name[20];
contact *found = NULL;
printf("enter the name of contact to search:");
scanf("%s",search_name);
for(i=0;i<current;i++)
{
if(strcmp(search_name,con[i].name) == 0)
{
found = &con[i];
break;
}
}
if(found)
{
// printf("Enter the new contact Information:\n");
printf("1.name\n2.phone number\n3.email\n4.age\n5.address\n");
int opt;
printf("enter the option:");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("Enter the contact details:\n");
found->index = con->index;
printf("ENTER THE NAME: ");
scanf(" %[^\n]", found->name); // Space before %[^\n] is important
printf("NAME updated sucessfully.\n");
break;
case 2:
printf("ENTER THE PHONE NUMBER: ");
scanf(" %[^\n]", found->number);
// Clear input buffer to avoid skipping next input
while (getchar() != '\n');
printf("NUMBER updated sucessfully.\n");
break;
case 3:
printf("ENTER THE EMAIL: ");
scanf(" %[^\n]",found->mail); // Space before %[^\n] is important
printf("MAIL updated sucessfully.\n");
break;
case 4:
printf("ENTER THE AGE: ");
scanf("%d", &found->age);
// Clear input buffer to prepare for the next iteration
while (getchar() != '\n');
printf("AGE updated sucessfully.\n");
break;
case 5:
printf("ENTER THE ADDRESS: ");
scanf(" %[^\n]", found->address);
printf("address updated sucessfully.\n");
break;
default:
puts("enter the input properly,");
break;
}
}
else
{
printf("contact not found.\n");
}
}
void delete_contacts(contact *con)
{
int i,j;
char search_name[20];
contact *found = NULL;
printf("enter the name of contact to search:");
scanf("%s",search_name);
for(i=0;i<current;i++)
{
if(strcmp(search_name,con[i].name) == 0)
{
found = &con[i];
break;
}
}
if(found)
{
for(j=i;j<current-1;j++)
{
con[j] = con[j+1];
}
current--;
printf("Contact deleted succesfully.\n");
}
else
{
printf("Contact not found.\n");
}
}
void load_contacts(const char *bookname,contact *con)
{
FILE *fptr = fopen(bookname,"r");
if(fptr)
{
while(current < MAX_CONTACTS && fread(&con[current],sizeof(contact),1,fptr))
{
current++;
}
fclose(fptr);
}
// else
//{
// printf("ERROR:THE FILE DOESNOT OPEN TO READ THE CONTACTS.\n");
// }
}
void save_contacts(const char *bookname,contact *con)
{
FILE *fptr = fopen(bookname,"w");
if(fptr)
{
fwrite(con,sizeof(contact),current,fptr);
fclose(fptr);
}
else
{
printf("ERROR: THE FILE DOESNOT OPEN TO WRITE THE DATA.");
}
}