-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphonebook.c
167 lines (137 loc) · 3.88 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 50
#define MAX_PHONE_LEN 15
#define FILE_NAME "phonebook.dat"
typedef struct {
char name[MAX_NAME_LEN];
char phone[MAX_PHONE_LEN];
} Contact;
// Function prototypes
void addContact();
void viewContacts();
void updateContact();
void deleteContact();
int main() {
int choice;
do {
printf("\n--- Phonebook ---\n");
printf("1. Add Contact\n");
printf("2. View Contacts\n");
printf("3. Update Contact\n");
printf("4. Delete Contact\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: addContact(); break;
case 2: viewContacts(); break;
case 3: updateContact(); break;
case 4: deleteContact(); break;
case 5: printf("Exiting the program.\n"); break;
default: printf("Invalid choice! Please try again.\n");
}
} while (choice != 5);
return 0;
}
void addContact() {
FILE *file = fopen(FILE_NAME, "ab");
if (file == NULL) {
perror("Error opening file");
return;
}
Contact contact;
printf("Enter name: ");
getchar();
fgets(contact.name, MAX_NAME_LEN, stdin);
contact.name[strcspn(contact.name, "\n")] = '\0';
printf("Enter phone number: ");
fgets(contact.phone, MAX_PHONE_LEN, stdin);
contact.phone[strcspn(contact.phone, "\n")] = '\0';
fwrite(&contact, sizeof(Contact), 1, file);
fclose(file);
printf("Contact added successfully!\n");
}
void viewContacts() {
FILE *file = fopen(FILE_NAME, "rb");
if (file == NULL) {
perror("Error opening file");
return;
}
Contact contact;
printf("\n--- Contacts ---\n");
while (fread(&contact, sizeof(Contact), 1, file)) {
printf("Name: %s, Phone: %s\n", contact.name, contact.phone);
}
fclose(file);
}
void updateContact() {
FILE *file = fopen(FILE_NAME, "rb+");
if (file == NULL) {
perror("Error opening file");
return;
}
char name[MAX_NAME_LEN];
printf("Enter the name of the contact to update: ");
getchar();
fgets(name, MAX_NAME_LEN, stdin);
name[strcspn(name, "\n")] = '\0';
Contact contact;
int found = 0;
long position;
while (fread(&contact, sizeof(Contact), 1, file)) {
if (strcmp(contact.name, name) == 0) {
found = 1;
position = ftell(file) - sizeof(Contact);
break;
}
}
if (found) {
fseek(file, position, SEEK_SET);
printf("Enter new phone number: ");
fgets(contact.phone, MAX_PHONE_LEN, stdin);
contact.phone[strcspn(contact.phone, "\n")] = '\0';
fwrite(&contact, sizeof(Contact), 1, file);
printf("Contact updated successfully!\n");
} else {
printf("Contact not found.\n");
}
fclose(file);
}
void deleteContact() {
FILE *file = fopen(FILE_NAME, "rb");
if (file == NULL) {
perror("Error opening file");
return;
}
FILE *tempFile = fopen("temp.dat", "wb");
if (tempFile == NULL) {
perror("Error opening temporary file");
fclose(file);
return;
}
char name[MAX_NAME_LEN];
printf("Enter the name of the contact to delete: ");
getchar();
fgets(name, MAX_NAME_LEN, stdin);
name[strcspn(name, "\n")] = '\0';
Contact contact;
int found = 0;
while (fread(&contact, sizeof(Contact), 1, file)) {
if (strcmp(contact.name, name) != 0) {
fwrite(&contact, sizeof(Contact), 1, tempFile);
} else {
found = 1;
}
}
fclose(file);
fclose(tempFile);
remove(FILE_NAME);
rename("temp.dat", FILE_NAME);
if (found) {
printf("Contact deleted successfully!\n");
} else {
printf("Contact not found.\n");
}
}