#include #include #include FILE *fd; struct enreg { char *nom; char *tel; //pour pouvoir allouer et libérer la mémoire }; int question(char *texte) { char reponse=' '; while (reponse!='o' && reponse!='n') { printf("%s",texte); reponse = getchar(); printf("\n"); /*convertion des majuscules en minuscules*/ if (reponse=='O') reponse='o'; if (reponse=='N') reponse='n'; if (reponse!='o' && reponse!='n') printf("!!!repondre par O ou N!!!\n"); } if (reponse=='o') return 1; //1 c'est vrai else return 0; //0 c'est faux } /*création d'une nouvelle fiche*/ void creation() { /*variable locales*/ struct enreg *fiche = malloc(sizeof(struct enreg)); fiche->nom = malloc(30 * sizeof(char)); fiche->tel = malloc(10 * sizeof(char));//les fameuses allocations int reponse; /*demande du nom et du numero de téléphone*/ printf("...Creation d'une fiche...\n"); printf("Nom ? "); scanf("%s",fiche->nom);//fiche->nom est une adresse donc c'est ok printf("Numero de telephone ? "); scanf("%s",fiche->tel);//... voir plus haut /*Demande confirmation d'enregistrement*/ reponse = question("Enregistrer la fiche (O/N) ?"); /*si NON, on sort directement*/ if(reponse)//si 1 on enregistre, voir la fonction question, sinon on on va.... { /*se placer à la fin du fichier*/ fseek(fd,0,2); /*sinon on enregistre à la fin du fichier*/ fprintf(fd,"%s %s\n",fiche->nom,fiche->tel); printf("Fiche enregistrée\n");//pour dire que c'est dedans } //.....ICI free(fiche->nom);//dans tous les cas on libère free(fiche->tel); free(fiche); //fin de la fonction on sort } void rechercher() { /*variable locales*/ struct enreg *fiche = malloc(sizeof(struct enreg));//pour pouvoir... fiche->nom = malloc(30 * sizeof(char));//lire notre fichier... fiche->tel = malloc(10 * sizeof(char));//et y placer nos variables pour les comparer... char *nom = malloc(30*sizeof(char));//à notre chaine allouer dynamiquement int err; int reponse; /*demande le nom à rechercher*/ printf("...rechercher une fiche...\n"); printf("nom a chercher ? "); scanf("%s",nom); /*positionnement au début du fichier*/ fseek(fd,0,0); /*lecture d'une fiche*/ err = fscanf(fd,"%s%s",fiche->nom,fiche->tel); /*si fin du fichier ou erreur*/ while (err > 0) { /*si la fiche correspond*/ if (strcmp(nom, fiche->nom) == 0) printf("nom:%s telephone:%s\n",fiche->nom,fiche->tel); /*on lit une autre fiche*/ err=fscanf(fd,"%s%s",fiche->nom,fiche->tel); } free(fiche->nom);//...libération free(fiche->tel); free(fiche); free(nom); reponse = question("continuer la recherche (O/N)?"); if (reponse)//voir plus haut rechercher();//on retourne dans la fonction /*fin*/ } int menu() { int choix=0; while (choix<1 || choix>3) { printf("menu : \n"); printf("1 - creation d'une nouvelle fiche\n"); printf("2 - recherche d'une fiche\n"); printf("3 - quitter\n"); printf("votre choix ? "); scanf("%d",&choix); if (choix<1 || choix>3) printf("!!!entrez 1, 2 ou 3 !!!\n"); } return choix; } /*programme principale*/ int main() { int choix=0; fd=fopen("rep.dat","a+"); printf("...REPERTOIRE TELEPHONIQUE...\n"); while (choix!=3) { choix=menu(); switch (choix)/*suivant le choix*/ { case 1 : creation();/*Cas 1, accéder àla fonction création*/ break; case 2 : rechercher();/*cas 2, accéder à la fonction rechercher*/ break; default : break;/*quitter (ne rien faire)*/ } } fclose(fd);/*Fermeture du fichier*/ /*Fin*/ printf("Merci, et au revoir\n"); return 0;//main doit en général retourner 0 pour "dire" cela s'est bien passé... }