diff --git a/base/main.c b/base/main.c new file mode 100644 index 0000000..9958c55 --- /dev/null +++ b/base/main.c @@ -0,0 +1,108 @@ +#include +#include +#include + +#define MAX_CONTACTS 100 + +// Structure d'un contact +typedef struct { + char nom[50]; + char prenom[50]; + char telephone[15]; +} Contact; + +// Prototypes des fonctions +void ajouterContact(Contact *contacts, int *nbContacts); +void afficherContacts(Contact *contacts, int nbContacts); +void sauvegarderContacts(Contact *contacts, int nbContacts, const char *filename); +void chargerContacts(Contact *contacts, int *nbContacts, const char *filename); + +int main() { + Contact contacts[MAX_CONTACTS]; + int nbContacts = 0; + chargerContacts(contacts, &nbContacts, "contacts.txt"); + + int choix; + do { + printf("\n1. Ajouter un contact\n"); + printf("2. Afficher les contacts\n"); + printf("3. Quitter\n"); + printf("Votre choix : "); + scanf("%d", &choix); + getchar(); // Pour éviter le problème de buffer avec scanf + + switch (choix) { + case 1: + ajouterContact(contacts, &nbContacts); + sauvegarderContacts(contacts, nbContacts, "contacts.txt"); + break; + case 2: + afficherContacts(contacts, nbContacts); + break; + case 3: + printf("Au revoir !\n"); + break; + default: + printf("Choix invalide, veuillez réessayer.\n"); + } + } while (choix != 3); + + return 0; +} + +// Fonction pour ajouter un contact +void ajouterContact(Contact *contacts, int *nbContacts) { + if (*nbContacts >= MAX_CONTACTS) { + printf("Le carnet d'adresses est plein !\n"); + return; + } + + printf("Nom : "); + fgets(contacts[*nbContacts].nom, 50, stdin); + strtok(contacts[*nbContacts].nom, "\n"); + + printf("Prénom : "); + fgets(contacts[*nbContacts].prenom, 50, stdin); + strtok(contacts[*nbContacts].prenom, "\n"); + + printf("Téléphone : "); + fgets(contacts[*nbContacts].telephone, 15, stdin); + strtok(contacts[*nbContacts].telephone, "\n"); + + (*nbContacts)++; +} + +// Fonction pour afficher tous les contacts +void afficherContacts(Contact *contacts, int nbContacts) { + if (nbContacts == 0) { + printf("Aucun contact enregistré.\n"); + return; + } + for (int i = 0; i < nbContacts; i++) { + printf("%d. %s %s - %s\n", i + 1, contacts[i].nom, contacts[i].prenom, contacts[i].telephone); + } +} + +// Fonction pour sauvegarder les contacts dans un fichier +void sauvegarderContacts(Contact *contacts, int nbContacts, const char *filename) { + FILE *file = fopen(filename, "w"); + if (!file) { + printf("Erreur d'ouverture du fichier.\n"); + return; + } + for (int i = 0; i < nbContacts; i++) { + fprintf(file, "%s;%s;%s\n", contacts[i].nom, contacts[i].prenom, contacts[i].telephone); + } + fclose(file); +} + +// Fonction pour charger les contacts depuis un fichier +void chargerContacts(Contact *contacts, int *nbContacts, const char *filename) { + FILE *file = fopen(filename, "r"); + if (!file) return; + + while (fscanf(file, "%49[^;];%49[^;];%14[^\n]\n", contacts[*nbContacts].nom, contacts[*nbContacts].prenom, contacts[*nbContacts].telephone) == 3) { + (*nbContacts)++; + } + fclose(file); +}