Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- //functions protoype
- void Menu(FILE *file);
- void Add_Number(FILE*file);
- void Show_Numbers(FILE *file);
- void Search_Number(FILE *file);
- //Define a struct
- typedef struct {
- char firstName[20];
- char lastName[20];
- unsigned long number ;
- }person;
- //Main program
- int main()
- {
- FILE *file = NULL;
- //file = fopen("contacts.txt", "a+");
- Menu(file);
- return 0;
- }
- void Add_Number(FILE *file)
- {
- person newUser;
- file = fopen("contacts.txt", "a");
- if (file != NULL)
- {
- printf("NOM:n");
- scanf("%s",newUser.firstName);
- fflush(stdin);
- printf("Prenom:n");
- scanf("%s",newUser.lastName);
- fflush(stdin);
- printf("Numero:n");
- scanf("%lu",&(newUser.number));
- fflush(stdin);
- fprintf(file, "n%st%st%lu", newUser.firstName, newUser.lastName, newUser.number);
- fflush(stdin);
- printf("Success");
- }
- else
- {
- printf("Erreur d'ouverture de fichier");
- exit(-1);
- }
- fclose(file);
- }
- void Show_Numbers(FILE*file)//FILE *file)
- {
- person user;
- char s[100];
- file = fopen("contacts.txt", "a+");
- printf("%s", user.firstName);
- if (file != NULL)
- {
- fseek(file,1,0);
- while (fgets(s,1000,file)!=NULL)
- {
- printf("%s",s);
- }
- //while (fgets(test, 100, file) != NULL /*EOF*/) // On lit le fichier tant qu'on ne reçoit pas d'erreur (NULL)
- /* {
- printf("%sn", test); // On affiche la chaîne qu'on vient de lire
- }*/
- fclose(file);
- /*
- fg(file, "%s %s %lun", test);//, user.lastName, user.number);
- printf("Nom: %stt", test);//user.firstName);
- // printf("Prenom: %stt", user.lastName);
- // printf("Numero: %lun", user.number);*/
- }
- else
- {
- printf("Erreur d'ouverture de fichier");
- exit(-1);
- }
- fclose(file);
- }
- void Search_Number(FILE *file)
- {
- file = fopen("contacts.txt", "a+");
- char userToFined[20];
- person user;
- //test if file not NULL
- if(file != NULL)
- {
- //input
- puts("saisir le numero a cherchern");
- gets(userToFined);
- //read from the begging
- rewind(file);
- do
- {
- fscanf(file, "%s %s %lu", user.firstName, user.lastName, &user.number);
- }
- while (user.firstName != userToFined && !feof(file));
- if(user.firstName == userToFined)
- {
- printf("Success ! %s est trouven", user.firstName);
- printf("Son est prenom: %stt", user.lastName);
- printf("Son numero est: %lun", user.number);
- }
- else
- {
- printf("%s est introuvable", userToFined);
- }
- }
- else
- {
- printf("Erreur d'ouverture de fichier");
- exit(-1);
- }
- fclose(file);
- }
- void Menu(FILE *file)
- {
- int choice;
- do
- {
- puts("n************************************************************ntt BIENVENUE ttn************************************************************");
- puts("t t **Choisir votre choix**t tn");
- puts("---1--- Ajouter un nouveau contactn");
- puts("---2--- Afficher le repertoiren");
- puts("---3--- rechercher un numeron");
- puts("---4--- Ajouter un nouveau contactn");
- puts("Votre Choix:");
- scanf("%d", &choice);
- switch (choice)
- {
- case 1:
- Add_Number(file); // Add user
- break;
- case 2:
- Show_Numbers(file); //Show repository
- break;
- case 3:
- Search_Number(file); //Search User
- break;
- case 4:
- exit(0);
- break;
- default:
- printf("Choix Invalid");
- break;
- }
- } while (choice != 4);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement