Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. //functions protoype
  5. void Menu(FILE *file);
  6. void Add_Number(FILE*file);
  7. void Show_Numbers(FILE *file);
  8. void Search_Number(FILE *file);
  9.  
  10.  
  11. //Define a struct
  12. typedef struct {
  13. char firstName[20];
  14. char lastName[20];
  15. unsigned long number ;
  16. }person;
  17.  
  18.  
  19. //Main program
  20. int main()
  21. {
  22. FILE *file = NULL;
  23. //file = fopen("contacts.txt", "a+");
  24. Menu(file);
  25. return 0;
  26. }
  27.  
  28.  
  29. void Add_Number(FILE *file)
  30. {
  31. person newUser;
  32.  
  33. file = fopen("contacts.txt", "a");
  34. if (file != NULL)
  35. {
  36. printf("NOM:n");
  37. scanf("%s",newUser.firstName);
  38. fflush(stdin);
  39.  
  40. printf("Prenom:n");
  41. scanf("%s",newUser.lastName);
  42. fflush(stdin);
  43.  
  44. printf("Numero:n");
  45. scanf("%lu",&(newUser.number));
  46. fflush(stdin);
  47.  
  48. fprintf(file, "n%st%st%lu", newUser.firstName, newUser.lastName, newUser.number);
  49. fflush(stdin);
  50. printf("Success");
  51.  
  52. }
  53.  
  54. else
  55. {
  56. printf("Erreur d'ouverture de fichier");
  57. exit(-1);
  58. }
  59. fclose(file);
  60. }
  61.  
  62.  
  63. void Show_Numbers(FILE*file)//FILE *file)
  64. {
  65. person user;
  66. char s[100];
  67. file = fopen("contacts.txt", "a+");
  68. printf("%s", user.firstName);
  69.  
  70. if (file != NULL)
  71. {
  72. fseek(file,1,0);
  73. while (fgets(s,1000,file)!=NULL)
  74. {
  75. printf("%s",s);
  76. }
  77.  
  78.  
  79. //while (fgets(test, 100, file) != NULL /*EOF*/) // On lit le fichier tant qu'on ne reçoit pas d'erreur (NULL)
  80. /* {
  81. printf("%sn", test); // On affiche la chaîne qu'on vient de lire
  82. }*/
  83.  
  84. fclose(file);
  85.  
  86.  
  87. /*
  88. fg(file, "%s %s %lun", test);//, user.lastName, user.number);
  89. printf("Nom: %stt", test);//user.firstName);
  90. // printf("Prenom: %stt", user.lastName);
  91. // printf("Numero: %lun", user.number);*/
  92.  
  93.  
  94. }
  95. else
  96. {
  97. printf("Erreur d'ouverture de fichier");
  98. exit(-1);
  99. }
  100. fclose(file);
  101. }
  102.  
  103.  
  104. void Search_Number(FILE *file)
  105. {
  106. file = fopen("contacts.txt", "a+");
  107. char userToFined[20];
  108. person user;
  109. //test if file not NULL
  110. if(file != NULL)
  111. {
  112. //input
  113. puts("saisir le numero a cherchern");
  114. gets(userToFined);
  115.  
  116. //read from the begging
  117. rewind(file);
  118. do
  119. {
  120. fscanf(file, "%s %s %lu", user.firstName, user.lastName, &user.number);
  121. }
  122. while (user.firstName != userToFined && !feof(file));
  123.  
  124. if(user.firstName == userToFined)
  125. {
  126. printf("Success ! %s est trouven", user.firstName);
  127. printf("Son est prenom: %stt", user.lastName);
  128. printf("Son numero est: %lun", user.number);
  129. }
  130. else
  131. {
  132. printf("%s est introuvable", userToFined);
  133. }
  134. }
  135. else
  136. {
  137. printf("Erreur d'ouverture de fichier");
  138. exit(-1);
  139. }
  140. fclose(file);
  141. }
  142.  
  143.  
  144. void Menu(FILE *file)
  145. {
  146. int choice;
  147. do
  148. {
  149. puts("n************************************************************ntt BIENVENUE ttn************************************************************");
  150. puts("t t **Choisir votre choix**t tn");
  151. puts("---1--- Ajouter un nouveau contactn");
  152. puts("---2--- Afficher le repertoiren");
  153. puts("---3--- rechercher un numeron");
  154. puts("---4--- Ajouter un nouveau contactn");
  155. puts("Votre Choix:");
  156. scanf("%d", &choice);
  157.  
  158. switch (choice)
  159. {
  160. case 1:
  161. Add_Number(file); // Add user
  162. break;
  163. case 2:
  164. Show_Numbers(file); //Show repository
  165. break;
  166. case 3:
  167. Search_Number(file); //Search User
  168. break;
  169. case 4:
  170. exit(0);
  171. break;
  172. default:
  173. printf("Choix Invalid");
  174. break;
  175. }
  176.  
  177. } while (choice != 4);
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement