#include "J2sae.h" int chargementCandidats(Candidat *tCandidats[], int *tMax) { FILE *flot; Candidat **tCand, *c; int i; flot = fopen("Candidats.don", "r"); if(flot == NULL) { printf("Erreur lors de l'ouverture du fichier candidat\n"); fclose(flot); return -1; } fscanf(flot, "%d", tMax); tCand = (Candidat **)malloc(sizeof(Candidat *) * (*tMax)); if(tCand == NULL) { printf("Erreur d'allocation mémoire tableau candidat\n"); exit(1); } for(i = 0; i < *tMax; i++) { c = lireCandidat(flot); tCand[i] = c; } tCandidats = tCand; } Candidat * lireCandidat(FILE *flot) { Candidat *c; Choix choix; int i = 0; c = (Candidat *)malloc(sizeof(Candidat)); if(c == NULL) { printf("Erreur d'allocation mémoire candidat\n"); exit(1); } fscanf(flot, "%d", &c->numeroC); fgets(c->nom, 31, flot); c->nom[strlen(c->nom) - 1] = '\0'; fgets(c->prenom, 31, flot); c->prenom[strlen(c->prenom) - 1] = '\0'; fscanf(flot, "%f%f%f%f", &c->notes[0], &c->notes[1], &c->notes[2], &c->notes[3]); fscanf(flot, "%d", &c->nombreChoix); c->tchoix = (Choix **)malloc(sizeof(Choix *) * c->nombreChoix); while(i <= c->nombreChoix) { c->tchoix[i] = lireChoix(flot); i++; } return c; } Choix * lireChoix(FILE *flot) { Choix *c; c = (Choix *)malloc(sizeof(Choix)); if(c == NULL) { printf("Erreur d'allocation mémoire choix\n"); exit(1); } fgets(c->ville, 31, flot); c->ville[strlen(c->ville) - 1] = '\0'; fgets(c->dep, 31, flot); c->dep[strlen(c->dep) - 1] = '\0'; fscanf(flot, "%d%d", &c->decisionResp, &c->decisionCand); return c; } void globale(void) { Candidat **tCandidats, c; int tMax; chargementCandidats(tCandidats, &tMax); afficherCandidat(*tCandidats[0]); } void afficherCandidat(Candidat c) { printf("%d\t%s\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t%d\n", c.numeroC, c.nom, c.prenom, c.notes[0], c.notes[1], c.notes[2], c.notes[3], c.nombreChoix); }