-
Notifications
You must be signed in to change notification settings - Fork 0
/
phonebook.c
125 lines (112 loc) · 3.22 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#define NAME_LENGTH 20
#define NUMBER_LENGTH 20
#define PHONEBOOK_LENGTH 100
#define COMMAND_LENGTH 5
/**
* Phonebook is a simple phonebook application. Store pairs of names and numbers and then look them up.
*/
/**
* A single contact, which has a name and phone number.
*/
struct contact {
char name[NAME_LENGTH];
char number[NUMBER_LENGTH];
};
typedef struct contact Contact;
/**
* The phonebook, which stores a number of contacts and keeps track of how many contacts it contains.
*/
struct phonebook {
int num_contacts;
Contact contacts[PHONEBOOK_LENGTH];
};
typedef struct phonebook Phonebook;
/**
* Prompt the user for a name and number, then add them to the phonebook.
*/
void add_contact(Phonebook *p) {
const int max = NAME_LENGTH + NUMBER_LENGTH + 2;
char name[NAME_LENGTH];
char number[NUMBER_LENGTH];
char line[max];
printf("Enter new contact details:\n");
printf("Name\tNumber\n");
fgets(line, max, stdin);
sscanf(line, "%s %s", name, number);
Contact new_contact;
strncpy(new_contact.name, name, sizeof(new_contact.name));
strncpy(new_contact.number, number, sizeof(new_contact.number));
p->contacts[p->num_contacts] = new_contact;
p->num_contacts++;
}
/**
* Prompt the user for a name, then display any matching records in the phonebook.
*/
void show_contact(Phonebook *p) {
char name[NAME_LENGTH];
char line[PHONEBOOK_LENGTH];
printf("Search for name: ");
fgets(line, NAME_LENGTH, stdin);
sscanf(line, "%s", name);
for (int i = 0; i < p->num_contacts; i++) {
if (strncmp(p->contacts[i].name, name, NAME_LENGTH) == 0) {
printf("Name: %s\tNum: %s\n", name, p->contacts[i].number);
return;
}
}
printf("Contact not found\n");
}
/**
* Display all records in the phonebook.
*/
void display_phonebook(Phonebook *p) {
for (int i = 0; i < p->num_contacts; i++) {
Contact contact = p->contacts[i];
printf("Name: %s Num: %s\n", contact.name, contact.number);
}
}
void write_file(Phonebook *p) {
FILE *fp;
fp = fopen("phonebook.txt", "r+");
if(fp != NULL) {
for(int i = 0; i < p->num_contacts; i++) {
Contact new_contact = p->contacts[i];
fputs(new_contact.name, fp);
fputs("\t", fp);
fputs(new_contact.number, fp);
fclose(fp);
}
}
}
/**
* Read commands from the user's input and dispatch to the appropriate functions.
*/
int main() {
Phonebook p;
p.num_contacts = 0;
printf("Welcome to the phone book. You can add or lookup contacts. (A/L/W).\n");
char line[COMMAND_LENGTH], command;
printf("> ");
fgets(line, COMMAND_LENGTH, stdin);
while (!feof(stdin)) {
sscanf(line, "%c", &command);
if (command == 'A') {
add_contact(&p);
} else if (command == 'L') {
show_contact(&p);
} else if (command == 'Q') {
break;
} else if (command == 'W') {
write_file(&p);
}
else {
printf("Invalid command. Try A or L.\n");
}
printf("> ");
fgets(line, COMMAND_LENGTH, stdin);
}
}