-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathphonebook.c
More file actions
274 lines (254 loc) · 5.87 KB
/
phonebook.c
File metadata and controls
274 lines (254 loc) · 5.87 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#define SEARCH 1
#define LIST 2
#define ADD 3
#define REMOVE 4
#define SAVE 5
#define LOAD 6
#define QUIT 7
#define MAXLINELENGTH 80
#define PHONEBOOKSIZE 1000
struct entry
{
char *name;
char *phoneNumber;
};
struct entry *phoneBook[PHONEBOOKSIZE];
void displayMenu(void);
void go(void);
int getMenuOption(void);
void doOption(int);
void listAllEntries(void);
void searchForEntry(void);
void addEntry(void);
void removeEntry(void);
void savePhoneBook(void);
void loadPhoneBook(void);
char* readString(char*);
void removeNewLine(char*);
struct entry *createEntry(char*, char*);
void initialiseSampleData(void);
void go()
{
while (true)
{
displayMenu();
int option = getMenuOption();
if (option == QUIT)
{
break;
}
else
{
doOption(option);
}
}
}
void displayMenu()
{
printf("\nPhone Book\n");
printf("%i. Search for a phone number\n", SEARCH);
printf("%i. List all entries in the phone book\n", LIST);
printf("%i. Add a new entry\n", ADD);
printf("%i. Remove an entry\n", REMOVE);
printf("%i. Save the phone book to a file\n", SAVE);
printf("%i. Load a phone book from a file\n", LOAD);
printf("%i. Quit\n", QUIT);
}
int getMenuOption()
{
int option = 0;
char *input = readString("Enter selection: ");
sscanf(input,"%i",&option);
free(input);
return option;
}
void doOption(int option)
{
switch (option)
{
case SEARCH : searchForEntry(); break;
case LIST : listAllEntries(); break;
case ADD : addEntry(); break;
case REMOVE : removeEntry(); break;
case SAVE : savePhoneBook(); break;
case LOAD : loadPhoneBook(); break;
default : printf("\nSorry - don't recognise that option, try again\n");
}
}
void searchForEntry()
{
printf("\n\nSearch Phone Book\n");
char *name = readString("Enter the person's name: ");
int index = 0;
while (phoneBook[index] != NULL)
{
if (!strcmp(phoneBook[index]->name, name))
{
printf("The phone number is: %s\n", phoneBook[index]->phoneNumber);
free(name);
return;
}
index++;
}
printf("Sorry - no entry found\n");
free(name);
}
void listAllEntries()
{
if (phoneBook[0] == NULL)
{
printf("\nThere are no entries in the phone book\n");
return;
}
int index = 0;
while (phoneBook[index] != NULL)
{
printf("Entry %i: Name: %s Number: %s\n",
index + 1, phoneBook[index]->name, phoneBook[index]->phoneNumber);
index++;
}
}
void addEntry()
{
printf("\nAdd entry to phone book\n");
int index = -1;
while(phoneBook[++index] != NULL);
if (index > PHONEBOOKSIZE-2)
{
printf("Sorry, the phone book is full\n");
return;
}
char *name = readString("Enter the person's name: ");
char *number = readString("Enter the person's phone number: ");
struct entry *entry = createEntry(name, number);
phoneBook[index] = entry;
phoneBook[index+1] = NULL;
free(name);
free(number);
}
void removeEntry()
{
printf("\nRemove entry from phone book\n");
char *name = readString("Enter the name of the person to remove: ");
int index = 0;
while((phoneBook[index] != NULL) && (strcmp(phoneBook[index]->name, name)))
{
index++;
}
if (phoneBook[index] == NULL)
{
printf("No entry found for %s, nothing removed\n", name);
free(name);
return;
}
struct entry *removed = phoneBook[index];
do
{
phoneBook[index] = phoneBook[index+1];
} while (phoneBook[++index] != NULL);
free(removed->name);
free(removed->phoneNumber);
free(removed);
free(name);
}
void savePhoneBook()
{
printf("\nSave the phone book to a file\n");
char *fileName = readString("Enter the file name: ");
FILE *outputFile = fopen(fileName,"w");
if (outputFile == NULL)
{
printf("\nUnable to write to file - phone book not saved.\n");
free(fileName);
return;
}
int index = 0;
while (phoneBook[index] != NULL)
{
fprintf(outputFile, "%s\n", phoneBook[index]->name);
fprintf(outputFile, "%s\n", phoneBook[index]->phoneNumber);
index++;
}
fclose(outputFile);
free(fileName);
}
void loadPhoneBook()
{
printf("\nLoad a phone book from a file\n");
char *fileName = readString("Enter the file name: ");
FILE *inputFile = fopen(fileName,"r");
if (inputFile == NULL)
{
printf("\nUnable to read from file - phone book not loaded.\n");
free(fileName);
return;
}
int index = 0;
char name[MAXLINELENGTH];
char phoneNumber[MAXLINELENGTH];
while (index < PHONEBOOKSIZE-1)
{
fgets(name,MAXLINELENGTH-1,inputFile);
char *input = fgets(phoneNumber,MAXLINELENGTH-1,inputFile);
if (input == NULL)
{
break;
}
removeNewLine(name);
removeNewLine(phoneNumber);
phoneBook[index] = createEntry(name, phoneNumber);
index++;
}
phoneBook[index] = NULL;
fclose(inputFile);
free(fileName);
}
void removeNewLine(char *buffer)
{
size_t length = strlen(buffer);
if (length == 0)
{
return;
}
if (buffer[length-1] == '\n')
{
buffer[length-1] = '\0';
}
}
char* readString(char* prompt)
{
char buffer[MAXLINELENGTH];
printf("%s", prompt);
fgets(buffer, MAXLINELENGTH, stdin);
size_t inputLength = strlen(buffer);
char *input = calloc(sizeof(char), inputLength);
strncpy(input, buffer, inputLength-1);
input[inputLength-1] = '\0';
return input;
}
struct entry *createEntry(char* name, char* phoneNumber)
{
struct entry *entry = calloc(sizeof(struct entry),1);
entry->name = calloc(sizeof(char), strlen(name) + 1);
strcpy(entry->name,name);
entry->phoneNumber = calloc(sizeof(char), strlen(phoneNumber) + 1);
strcpy(entry->phoneNumber, phoneNumber);
return entry;
}
void initialiseSampleData()
{
phoneBook[0] = createEntry("Person 1", "12345");
phoneBook[1] = createEntry("Someone", "4567");
phoneBook[2] = NULL;
}
int main (void)
{
phoneBook[0] = NULL;
initialiseSampleData();
go();
return 0;
}