forked from malmhaug/C_AbsBegin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC11E4PhoneBook.c
70 lines (55 loc) · 1.54 KB
/
C11E4PhoneBook.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
/* Challenges Ch11E4 - PhoneBook */
// Rev. by Jim-Kristian Malmhaug | Date: 27 June 2015
#include <stdio.h>
#include <stdlib.h>
main() {
int response;
char lName[20] = {0};
char fName[20] = {0};
char number[20] = {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\tExit\n\n");
printf("Select an option: ");
scanf("%d", &response);
if ( response == 1 ) {
/* user is adding a new phone book entry – get the info */
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
goto ErrorHandler; //there is a file i/o error
}
else if ( response == 2 ) {
/* user wants to print the phone book */
pRead = fopen("phone_book.dat", "r");
if ( pRead != NULL ) {
printf("\nPhone Book Entries\n");
while ( !feof(pRead) ) {
fscanf(pRead, "%s %s %s", fName, lName, number);
if ( !feof(pRead) )
printf("\n%s %s\t%s", fName, lName, number);
} //end loop
printf("\n");
}
else
goto ErrorHandler; //there is a file i/o error
}
}while(response != 3);
exit(EXIT_SUCCESS); //exit program normally
ErrorHandler:
perror("The following error occurred");
exit(EXIT_FAILURE); //exit program with error
} //end main