diff --git a/Algo/tp/tp10/exe b/Algo/tp/tp10/exe new file mode 100755 index 0000000..9340d24 Binary files /dev/null and b/Algo/tp/tp10/exe differ diff --git a/Algo/tp/tp10/resultat.bin b/Algo/tp/tp10/resultat.bin new file mode 100644 index 0000000..16e858c Binary files /dev/null and b/Algo/tp/tp10/resultat.bin differ diff --git a/Algo/tp/tp10/resultat.don b/Algo/tp/tp10/resultat.don new file mode 100644 index 0000000..24592a6 --- /dev/null +++ b/Algo/tp/tp10/resultat.don @@ -0,0 +1,5 @@ +4 +Martin algo1 12.5 +Amélie maths 15.5 +Gilbert based 17.0 +Anne organ 10 \ No newline at end of file diff --git a/Algo/tp/tp10/testtp10.c b/Algo/tp/tp10/testtp10.c new file mode 100644 index 0000000..44add57 --- /dev/null +++ b/Algo/tp/tp10/testtp10.c @@ -0,0 +1,7 @@ +#include "tp10.h" + +int main(void) +{ + global(); + return 0; +} diff --git a/Algo/tp/tp10/tp10.c b/Algo/tp/tp10/tp10.c new file mode 100644 index 0000000..feb49c5 --- /dev/null +++ b/Algo/tp/tp10/tp10.c @@ -0,0 +1,99 @@ +#include "tp10.h" + +Resultat lireResultat(FILE *fe) +{ + Resultat c; + fscanf(fe, "%s\t%s\t%f", c.nom, c.cdmat, &c.moyenne); + return c; +} + +void afficheResultat(Resultat c) +{ + printf("%s\t%s\t%f\n", c.nom, c.cdmat, c.moyenne); +} + +Resultat *chargeTresultat(char *nomFic, int *nbres) +{ + Resultat *tab; + FILE *flot; + int i; + flot = fopen(nomFic, "r"); + if(flot == NULL) + { + printf("Pb d'ouverture du fichier resultat.don\n"); + exit(1); + } + fscanf(flot, "%d", nbres); + tab = (Resultat *)malloc(*nbres * sizeof(Resultat)); + if(tab == NULL) + { + printf("Pb, il n'y a rien dans le fichier resultat.don\n"); + exit(1); + } + for(i=0; i < *nbres; i++) + tab[i]=lireResultat(flot); + fclose(flot); + return tab; +} + +void afficheTresultat(Resultat *tab, int nbres) +{ + int i; + printf("\n"); + for(i = 0; i < nbres; i++) + afficheResultat(tab[i]); +} + +void sauveTresultat(Resultat *tRes, int nbres) +{ + FILE *flot; + int i; + flot = fopen("resultat.bin", "w"); + if(flot == NULL) + { + printf("Pb d'ouverture du fichier resultat.bin\n"); + exit(1); + } + fprintf(flot, "%d\n", nbres); + fwrite(tRes, sizeof(Resultat), nbres, flot); + fclose(flot); +} + +Resultat *restaureTresultat(char *nomfichier, int *nbres) +{ + Resultat *tab; + FILE *flot; + flot = fopen(nomfichier, "r"); + if(flot == NULL) + { + printf("Pb d'ouverture du fichier resultat.don\n"); + exit(1); + } + fscanf(flot, "%d\n", nbres); + tab = (Resultat *)malloc(*nbres * sizeof(Resultat)); + if(tab == NULL) + { + printf("Pb, il n'y a rien dans le fichier resultat.bin\n"); + exit(1); + } + fread(tab, sizeof(Resultat), *nbres, flot); + fclose(flot); + return tab; +} + +void global(void) +{ + Resultat *tab; + int nb; + char nomFic[20]; + printf("Entrez le nom du fichier à lire : "); + scanf("%s", nomFic); + tab = chargeTresultat(nomFic, &nb); + afficheTresultat(tab, nb); + sauveTresultat(tab, nb); + free(tab); + tab = restaureTresultat("resultat.bin", &nb); + afficheTresultat(tab, nb); + free(tab); +} + diff --git a/Algo/tp/tp10/tp10.h b/Algo/tp/tp10/tp10.h new file mode 100644 index 0000000..ce47418 --- /dev/null +++ b/Algo/tp/tp10/tp10.h @@ -0,0 +1,17 @@ +#include +#include +#include + +typedef struct{ + char nom[21]; + char cdmat[6]; + float moyenne; +} Resultat; + +Resultat lireResultat(FILE *fe); +void afficheResultat(Resultat c); +Resultat *chargeTresultat(char *nomFic, int *nbres); +void afficheTresultat(Resultat *tab, int nbres); +void sauveTresultat(Resultat *tRes, int nbres); +Resultat *restaureTresultat(char *nomFic, int *nbres); +void global(void);