forked from malmhaug/C_AbsBegin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC11E5PhoneBook.c
204 lines (166 loc) · 4.33 KB
/
C11E5PhoneBook.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* Challenges Ch11E5 - PhoneBook */
// Rev. by Jim-Kristian Malmhaug | Date: 12 July 2015
#include <stdio.h>
#include <stdlib.h>
int addEntry(); //Function prototype for adding entry to phone book
int printEntries(); //Function prototype for printing entries in phone book
int editEntries(); //Function prototype for editing phone book
int deleteEntries(); //Function prototype for deleting entries
main() {
int response;
signed int iStatus = 0;
FILE *pWrite;
FILE *pRead;
do{
printf("\n\tPhone Book\n");
printf("\n1\tAdd phone book entry\n");
printf("2\tPrint phone book\n");
printf("3\tEdit phone book\n");
printf("4\tDelete entry in phone book\n");
printf("5\tExit\n\n");
printf("Select an option: ");
scanf("%d", &response);
if ( response == 1 ) {
addEntry();
}
else if ( response == 2 ) {
printEntries();
}
else if ( response == 3 ) {
editEntries();
}
else if ( response == 4 ) {
deleteEntries();
}
else if (iStatus == -1 ) {
goto ErrorHandler;
}
}while(response != 5);
system("clear");
exit(EXIT_SUCCESS); //exit program normally
ErrorHandler:
perror("The following error occurred");
exit(EXIT_FAILURE); //exit program with error
} //end main
//----------------------------------------------------------------
//----------------------FUNCTION DEFINITIONS----------------------
//----------------------------------------------------------------
/*----------Add new phone book entry----------*/
int addEntry(){
char lName[20] = {0};
char fName[20] = {0};
char number[20] = {0};
FILE *pWrite;
printf("\nEnter first name: ");
scanf("%s", fName);
printf("\nEnter last name: ");
scanf("%s", lName);
printf("\nEnter phone number: ");
scanf("%s", number);
pWrite = fopen("phone_book.dat", "a");
if ( pWrite != NULL ) {
fprintf(pWrite, "%s %s %s\n", fName, lName, number);
fclose(pWrite);
}
else
fclose(pWrite);
return -1; //there is a file i/o error
}
/*----------Print phone book entries----------*/
int printEntries(){
char lName[20] = {0};
char fName[20] = {0};
char number[20] = {0};
int iEntryNr = 0;
FILE *pRead;
pRead = fopen("phone_book.dat", "r");
if ( pRead != NULL ) {
printf("\nPhone Book Entries\n");
while ( !feof(pRead) ) {
iEntryNr += 1;
fscanf(pRead, "%s %s %s", fName, lName, number);
if ( !feof(pRead) )
printf("\n%d. %s %s\t%s", iEntryNr, fName, lName, number);
} //end loop
printf("\n");
}
else
fclose(pRead);
return -1; //there is a file i/o error
}
/*----------Edit phone book entries----------*/
int editEntries() {
int iSelect = 0;
int iCount = 0;
char lName_2[20] = {0};
char fName_2[20] = {0};
char number_2[20] = {0};
char cStoreRecord[70] = {0};
FILE *pRead;
FILE *pWriteTemp;
pRead = fopen("phone_book.dat", "r");
pWriteTemp = fopen("temp.dat", "a");
system("clear");
printEntries();
printf("\n\nWhich entry would you like to change?");
printf("\nEdit nr ---> ");
scanf("%d", &iSelect);
printf("\nEnter first name: ");
scanf("%s", fName_2);
printf("\nEnter last name: ");
scanf("%s", lName_2);
printf("\nEnter phone number: ");
scanf("%s", number_2);
if ( pWriteTemp != NULL || pRead != NULL) {
while( !feof(pRead) ) {
if (iCount == iSelect) {
fprintf(pWriteTemp, "%s %s %s\n", fName_2, lName_2, number_2);
}
else
fprintf(pWriteTemp, "%s", cStoreRecord);
iCount += 1;
fgets(cStoreRecord, 70, pRead);
}
}
else{
fclose(pRead);
fclose(pWriteTemp);
return -1; //there is a file i/o error
}
fclose(pRead);
fclose(pWriteTemp);
remove("phone_book.dat");
rename("temp.dat","phone_book.dat");
}
/*----------Delete phone book entries----------*/
int deleteEntries() {
int iSelect = 0;
int iCount = 0;
char cStoreRecord[70] = {0};
FILE *pRead;
FILE *pWriteTemp;
pRead = fopen("phone_book.dat", "r");
pWriteTemp = fopen("temp.dat", "a");
system("clear");
printEntries();
printf("\n\nWhich entry would you like to delete?");
printf("\nDelete nr ---> ");
scanf("%d", &iSelect);
if ( pWriteTemp != NULL || pRead != NULL) {
while( !feof(pRead) ) {
if (iCount != iSelect)
fprintf(pWriteTemp, "%s", cStoreRecord);
iCount += 1;
fgets(cStoreRecord, 70, pRead);
}
}
else{
fclose(pRead);
fclose(pWriteTemp);
return -1; //there is a file i/o error
}
fclose(pRead);
fclose(pWriteTemp);
remove("phone_book.dat");
rename("temp.dat","phone_book.dat");
}