Created
May 30, 2017 10:02
-
-
Save mamins1376/8123149466430e7b9a185182379c45a7 to your computer and use it in GitHub Desktop.
A Really Simple Phonebook in pure C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Phonebook | |
* Mohammad Amin Sameti (951019034) | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
typedef struct { | |
char first_name[20]; | |
char last_name[20]; | |
char phone[20]; | |
} Contact; | |
Contact contacts[60]; | |
int contacts_count; | |
void add_contact() { | |
Contact *contact = contacts + contacts_count; | |
puts("Enter First Name:"); | |
scanf("%s", &contact->first_name); | |
puts("Enter Last Name: "); | |
scanf("%s", &contact->last_name); | |
puts("Enter Phone Number: "); | |
scanf("%s", &contact->phone); | |
puts("Contact Saved."); | |
contacts_count++; | |
} | |
void edit_contact(int index) { | |
Contact *contact = contacts + index; | |
if (index >= contacts_count || index < 0) { | |
puts("ERROR: Invalid Contact ID"); | |
return; | |
} | |
printf("Current First Name: %s\n", contact->first_name); | |
puts("Enter First Name:"); | |
scanf("%s", &contact->first_name); | |
printf("Current Last Name: %s\n", contact->last_name); | |
puts("Enter Last Name: "); | |
scanf("%s", &contact->last_name); | |
printf("Current Phone Number: %s\n", contact->phone); | |
puts("Enter Phone Number: "); | |
scanf("%s", &contact->phone); | |
puts("Contact Edited."); | |
} | |
void delete_contact(int index) { | |
Contact *contact, *last; | |
if (index >= contacts_count || index < 0) { | |
puts("ERROR: Invalid Contact ID"); | |
return; | |
} | |
contact = contacts + index; | |
last = contacts + contacts_count - 1; | |
strcpy(contact->first_name, last->first_name); | |
strcpy(contact->last_name, last->last_name); | |
strcpy(contact->phone, last->phone); | |
contacts_count--; | |
} | |
void show_contacts() { | |
int i; | |
Contact *contact; | |
puts("ID | Full Name | Phone Number"); | |
for (i = 0; i < contacts_count; i++) { | |
contact = contacts + i; | |
printf("%i | %s %s | %s\n", i + 1, contact->first_name, contact->last_name, contact->phone); | |
} | |
} | |
char read_string(char *s, FILE *file) { | |
char i, c; | |
i = 0; | |
s[0] = 0; | |
while (1) { | |
c = fgetc(file); | |
if (c == 0) { | |
s[i] = 0; | |
return i; | |
} else if (c == EOF) { | |
return -1; | |
} else { | |
s[i] = c; | |
i++; | |
} | |
} | |
} | |
void load_contacts() { | |
char r, *s; | |
FILE *file = fopen("contacts.txt", "r"); | |
if (!file) | |
return; | |
contacts_count = 0; | |
while (1) { | |
s = contacts[contacts_count].first_name; | |
r = read_string(s, file); | |
if (r < 0) | |
break; | |
s = contacts[contacts_count].last_name; | |
read_string(s, file); | |
s = contacts[contacts_count].phone; | |
read_string(s, file); | |
contacts_count++; | |
} | |
fclose(file); | |
} | |
void save_contacts() { | |
int i; | |
Contact *contact; | |
FILE *file = fopen("contacts.txt", "w"); | |
if (!file) | |
return; | |
for (i = 0; i < contacts_count; i++) { | |
contact = contacts + i; | |
fputs(contact->first_name, file); | |
fputc(0, file); | |
fputs(contact->last_name, file); | |
fputc(0, file); | |
fputs(contact->phone, file); | |
fputc(0, file); | |
} | |
fclose(file); | |
} | |
int get_command() { | |
char c; | |
puts("Enter Command:"); | |
puts(" a) Add"); | |
puts(" e) Edit"); | |
puts(" d) Delete"); | |
puts(" s) Show"); | |
puts(" q) Quit"); | |
printf("\n: "); | |
scanf("%s", &c); | |
switch (c) { | |
case 'a': | |
add_contact(); | |
break; | |
case 'e': | |
printf("Enter Contact ID: "); | |
scanf("%i", &c); | |
edit_contact(c - 1); | |
break; | |
case 'd': | |
printf("Enter Contact ID: "); | |
scanf("%i", &c); | |
delete_contact(c - 1); | |
break; | |
case 's': | |
show_contacts(); | |
break; | |
case 'q': | |
puts("Goodbye"); | |
return 0; | |
default: | |
puts("ERROR: Invalid Command"); | |
break; | |
} | |
puts(""); | |
return 1; | |
} | |
int main() { | |
load_contacts(); | |
while (get_command()); | |
save_contacts(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment