From 7d1cf291deda94e44be525236195d835f7c8d35b Mon Sep 17 00:00:00 2001 From: ache Date: Wed, 6 Feb 2019 08:02:45 +0100 Subject: Write contact list --- contactList.c | 19 +++++++++++++++++-- contactList.h | 13 +++---------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/contactList.c b/contactList.c index cbc3b24..4369926 100644 --- a/contactList.c +++ b/contactList.c @@ -63,8 +63,7 @@ int addContact(const contact* cnct) { memcpy(&contactList[nbContacts-1],cnct, sizeof *cnct); } - -int fillContactList(char* filename) { +int fillContactList(const char* filename) { FILE* file = fopen(filename, "r"); nbContacts = 0; @@ -167,7 +166,23 @@ int fillContactList(char* filename) { } } } + fclose(file); return 0; } +int writeContactList(const char* filename) { + FILE* file = fopen(filename, "w"); + + if( !file ) { + fprintf(stderr, "Unable to read contact file (%s)\n", filename); + return 1; + } + + for( int i = 0 ; i < nbContacts ; i++) { + fprintf(file, "%s;;%s;;%s\n", contactList[i].name, contactList[i].display_name, contactList[i].number); + } + fclose(file); + + return 0; +} diff --git a/contactList.h b/contactList.h index c5cf10c..6d72dee 100644 --- a/contactList.h +++ b/contactList.h @@ -22,12 +22,12 @@ typedef struct contact { // Params: The CSV filename (separator ';', unexcaped string, so ';' is forbiden in value) // Return: 0 if success, >0 if error -int fillContactList(char* cvs_file_in); +int fillContactList(const char* cvs_file_in); // Params: A valide filename // Return: 0 if success, >0 if error -int writeContactList(char* cvs_file_in); +int writeContactList(const char* cvs_file_in); // Find a contact // Params: A SIM Number @@ -50,14 +50,7 @@ int addContact(const contact*); // Delete a contact // Params: A String which is a valide contact SIM number // Return: 0 if success, >0 otherwise -int deleteContactFromNumber(const char*); - - -// Delete a contact from his name -// Params: A String which is a valide contact SIM number -// Return: 0 if success, >0 otherwise -int deleteContactFromName(const char*); - +int deleteContact(void); // Sort the contact list // Params : Nothing -- cgit v1.2.3