Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. /*****************************************
  2. CREER ET AFFICHER UN FICHIER DE TYPE TEXTE
  3. *****************************************/
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. FILE *fichier;
  10. char NOM_FICHIER[100] , BUFFER[100];
  11. int C, NB_ENREG;
  12.  
  13. typedef struct Date
  14. {
  15. int day;
  16. char month[100];
  17. int year;
  18. } Date;
  19.  
  20. typedef struct Fiche
  21. {
  22. char NOM_PERS[100];
  23. char PRENOM_PERS[100];
  24. Date *dateNaissance;
  25. } Fiche;
  26.  
  27. Date * ecrireDate(int _day, char *_month, int _year)
  28. {
  29. Date *retVal = malloc (sizeof (Date));
  30. if (retVal == NULL)
  31. return NULL;
  32. retVal->day = _day;
  33. strcpy(retVal->month, _month);
  34. retVal->year = _year;
  35. return retVal;
  36. }
  37.  
  38. Fiche * ecrireFiche(char *nom, char *prenom)
  39. {
  40. Fiche *retVal = malloc (sizeof (Fiche));
  41. if (retVal == NULL)
  42. return NULL;
  43. strcpy(retVal->NOM_PERS, nom);
  44. strcpy(retVal->PRENOM_PERS, prenom);
  45. return retVal;
  46. }
  47.  
  48. void lireDate(Date *date){
  49. printf("JOUR:%d MOIS:%s ANNEE:%d", date->day, date->month, date->year);
  50. }
  51.  
  52. void lireFiche(Fiche *fiche){
  53. printf("NOM:%s PRENON:%s ", fiche->NOM_PERS,fiche->PRENOM_PERS);
  54. lireDate(fiche->dateNaissance);
  55. printf("\n");
  56. }
  57.  
  58. int main()
  59. {
  60. /* Première partie */
  61. printf("Entrez le nom du fichier a creer :\n");
  62. scanf("%s", NOM_FICHIER);
  63. strcat(NOM_FICHIER, ".csv");
  64. //printf("%s\n",NOM_FICHIER);
  65. fichier = fopen(NOM_FICHIER,"w");
  66. printf("Nombre d'enregistrement a creer :\n");
  67. scanf("%d", &NB_ENREG);
  68. for(C=0; C<NB_ENREG; C++){
  69.  
  70. Fiche *fiche = malloc (sizeof (Fiche));
  71. fiche->dateNaissance = malloc (sizeof (Date));
  72. printf("Entrez le nom de la personne :\n");
  73. scanf("%s", fiche->NOM_PERS);
  74.  
  75. printf("Entrez le prenom de la personne :\n");
  76. scanf("%s", fiche->PRENOM_PERS);
  77.  
  78. //Date
  79. printf("Entrez le jour de naissance :\n");
  80. scanf("%d", &(fiche->dateNaissance->day)); //scanf("%d", &(*dateNaissance).day);
  81.  
  82. printf("Entrez le mois de naissance :\n");
  83. scanf("%s", fiche->dateNaissance->month);
  84.  
  85. printf("Entrez l'annee de naissance :\n");
  86. scanf("%d", &(fiche->dateNaissance->year));
  87.  
  88. //printf("%s;%s;%d;%s;%d", fiche->NOM_PERS,fiche->PRENOM_PERS,dateNaissance->day, dateNaissance->month, dateNaissance->year);
  89. fprintf(fichier,"%s;%s;%d;%s;%d\n", fiche->NOM_PERS,fiche->PRENOM_PERS,fiche->dateNaissance->day, fiche->dateNaissance->month, fiche->dateNaissance->year);
  90. }
  91. fclose(fichier);
  92.  
  93. /* Deuxième partie */
  94. fichier = fopen(NOM_FICHIER, "r");
  95. char delim[] = ";";
  96. for (C=0 ; !feof(fichier) ; C++)
  97. {
  98. char LINE[255];
  99. char *ptrNom, *prtPrenom, *ptrMois;
  100. int jour, year;
  101.  
  102. fscanf(fichier, "%s\n", LINE);
  103. ptrNom = strtok(LINE, delim);
  104. prtPrenom = strtok(NULL, delim);
  105. jour = atoi(strtok(NULL, delim));
  106. ptrMois = strtok(NULL, delim);
  107. year = atoi(strtok(NULL, delim));
  108. Fiche *fiche = ecrireFiche(ptrNom,prtPrenom);
  109.  
  110. fiche->dateNaissance =ecrireDate(jour, ptrMois, year);
  111. lireFiche(fiche);
  112. }
  113. fclose(fichier);
  114. return 0;
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement