-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
189 lines (161 loc) · 4.41 KB
/
main.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
#include <stdio.h>
#include <string.h>
struct Contact {
int id;
char name[50];
char phone[15];
char email[50];
char address[100];
};
void addContact(struct Contact *contacts, int *count, int *nextId) {
struct Contact newContact;
newContact.id = (*nextId)++;
printf("Enter Name: ");
scanf("%s", newContact.name);
printf("Enter Phone: ");
scanf("%s", newContact.phone);
printf("Enter Email: ");
scanf("%s", newContact.email);
printf("Enter Address: ");
scanf("%s", newContact.address);
contacts[(*count)++] = newContact;
FILE *file = fopen("contacts.txt", "a");
fprintf(file, "%d %s %s %s %s\n", newContact.id, newContact.name,
newContact.phone, newContact.email, newContact.address);
fclose(file);
printf("Contact added successfully!\n");
}
void listContacts(struct Contact *contacts, int count) {
if (count == 0) {
printf("No contacts found.\n");
return;
}
printf("Contact List:\n");
for (int i = 0; i < count; i++) {
printf("ID: %d\nName: %s\nPhone: %s\nEmail: %s\nAddress: %s\n\n",
contacts[i].id, contacts[i].name, contacts[i].phone,
contacts[i].email, contacts[i].address);
}
}
int findContactIndex(struct Contact *contacts, int count, int id) {
for (int i = 0; i < count; i++) {
if (contacts[i].id == id) {
return i;
}
}
return -1;
}
void updateContact(struct Contact *contacts, int *count) {
int id;
printf("Enter the ID of the contact you want to update: ");
scanf("%d", &id);
int index = findContactIndex(contacts, *count, id);
if (index == -1) {
printf("Contact not found.\n");
return;
}
int choice;
printf("Select the field to update:\n");
printf("1. Name\n");
printf("2. Phone\n");
printf("3. Email\n");
printf("4. Address\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the new name: ");
scanf("%s", contacts[index].name);
break;
case 2:
printf("Enter the new phone: ");
scanf("%s", contacts[index].phone);
break;
case 3:
printf("Enter the new email: ");
scanf("%s", contacts[index].email);
break;
case 4:
printf("Enter the new address: ");
scanf("%s", contacts[index].address);
break;
default:
printf("Invalid choice. No changes were made.\n");
}
FILE *file = fopen("contacts.txt", "w");
for (int i = 0; i < *count; i++) {
fprintf(file, "%d %s %s %s %s\n", contacts[i].id, contacts[i].name,
contacts[i].phone, contacts[i].email, contacts[i].address);
}
fclose(file);
printf("Contact updated successfully!\n");
}
void deleteContact(struct Contact *contacts, int *count) {
int id;
printf("Enter the ID of the contact you want to delete: ");
scanf("%d", &id);
int index = findContactIndex(contacts, *count, id);
if (index == -1) {
printf("Contact not found.\n");
return;
}
for (int i = index; i < *count - 1; i++) {
contacts[i] = contacts[i + 1];
}
(*count)--;
FILE *file = fopen("contacts.txt", "w");
for (int i = 0; i < *count; i++) {
fprintf(file, "%d %s %s %s %s\n", contacts[i].id, contacts[i].name,
contacts[i].phone, contacts[i].email, contacts[i].address);
}
fclose(file);
printf("Contact deleted successfully!\n");
}
int main() {
struct Contact contacts[100];
int count = 0;
int nextId = 1;
FILE *file = fopen("contacts.txt", "r");
if (file) {
while (fscanf(file, "%d %s %s %s %s", &contacts[count].id,
contacts[count].name, contacts[count].phone,
contacts[count].email, contacts[count].address) != EOF) {
count++;
if (contacts[count - 1].id >= nextId) {
nextId = contacts[count - 1].id + 1;
}
}
fclose(file);
}
int choice;
while (1) {
printf("\nContact Management System\n");
printf("1. Add Contact\n");
printf("2. Update Contact\n");
printf("3. Delete Contact\n");
printf("4. List Contacts\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addContact(contacts, &count, &nextId);
break;
case 2:
updateContact(contacts, &count);
break;
case 3:
deleteContact(contacts, &count);
break;
case 4:
listContacts(contacts, count);
break;
case 5:
printf("Goodbye!\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}