Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*****************************************
- CREER ET AFFICHER UN FICHIER DE TYPE TEXTE
- *****************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- FILE *fichier;
- char NOM_FICHIER[100] , BUFFER[100];
- int C, NB_ENREG;
- typedef struct Date
- {
- int day;
- char month[100];
- int year;
- } Date;
- typedef struct Fiche
- {
- char NOM_PERS[100];
- char PRENOM_PERS[100];
- Date *dateNaissance;
- } Fiche;
- Date * ecrireDate(int _day, char *_month, int _year)
- {
- Date *retVal = malloc (sizeof (Date));
- if (retVal == NULL)
- return NULL;
- retVal->day = _day;
- strcpy(retVal->month, _month);
- retVal->year = _year;
- return retVal;
- }
- Fiche * ecrireFiche(char *nom, char *prenom)
- {
- Fiche *retVal = malloc (sizeof (Fiche));
- if (retVal == NULL)
- return NULL;
- strcpy(retVal->NOM_PERS, nom);
- strcpy(retVal->PRENOM_PERS, prenom);
- return retVal;
- }
- void lireDate(Date *date){
- printf("JOUR:%d MOIS:%s ANNEE:%d", date->day, date->month, date->year);
- }
- void lireFiche(Fiche *fiche){
- printf("NOM:%s PRENON:%s ", fiche->NOM_PERS,fiche->PRENOM_PERS);
- lireDate(fiche->dateNaissance);
- printf("\n");
- }
- int main()
- {
- /* Première partie */
- printf("Entrez le nom du fichier a creer :\n");
- scanf("%s", NOM_FICHIER);
- strcat(NOM_FICHIER, ".csv");
- //printf("%s\n",NOM_FICHIER);
- fichier = fopen(NOM_FICHIER,"w");
- printf("Nombre d'enregistrement a creer :\n");
- scanf("%d", &NB_ENREG);
- for(C=0; C<NB_ENREG; C++){
- Fiche *fiche = malloc (sizeof (Fiche));
- fiche->dateNaissance = malloc (sizeof (Date));
- printf("Entrez le nom de la personne :\n");
- scanf("%s", fiche->NOM_PERS);
- printf("Entrez le prenom de la personne :\n");
- scanf("%s", fiche->PRENOM_PERS);
- //Date
- printf("Entrez le jour de naissance :\n");
- scanf("%d", &(fiche->dateNaissance->day)); //scanf("%d", &(*dateNaissance).day);
- printf("Entrez le mois de naissance :\n");
- scanf("%s", fiche->dateNaissance->month);
- printf("Entrez l'annee de naissance :\n");
- scanf("%d", &(fiche->dateNaissance->year));
- //printf("%s;%s;%d;%s;%d", fiche->NOM_PERS,fiche->PRENOM_PERS,dateNaissance->day, dateNaissance->month, dateNaissance->year);
- fprintf(fichier,"%s;%s;%d;%s;%d\n", fiche->NOM_PERS,fiche->PRENOM_PERS,fiche->dateNaissance->day, fiche->dateNaissance->month, fiche->dateNaissance->year);
- }
- fclose(fichier);
- /* Deuxième partie */
- fichier = fopen(NOM_FICHIER, "r");
- char delim[] = ";";
- for (C=0 ; !feof(fichier) ; C++)
- {
- char LINE[255];
- char *ptrNom, *prtPrenom, *ptrMois;
- int jour, year;
- fscanf(fichier, "%s\n", LINE);
- ptrNom = strtok(LINE, delim);
- prtPrenom = strtok(NULL, delim);
- jour = atoi(strtok(NULL, delim));
- ptrMois = strtok(NULL, delim);
- year = atoi(strtok(NULL, delim));
- Fiche *fiche = ecrireFiche(ptrNom,prtPrenom);
- fiche->dateNaissance =ecrireDate(jour, ptrMois, year);
- lireFiche(fiche);
- }
- fclose(fichier);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement