From 003797fe640445246f82b72319b403ccef1243b5 Mon Sep 17 00:00:00 2001 From: d3friski Date: Fri, 6 Jan 2023 14:13:56 +0100 Subject: [PATCH 1/2] Ajout fichier avec fonction de chargement --- source/chargEtSauvFich.c | 77 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 source/chargEtSauvFich.c diff --git a/source/chargEtSauvFich.c b/source/chargEtSauvFich.c new file mode 100644 index 0000000..74ba2cd --- /dev/null +++ b/source/chargEtSauvFich.c @@ -0,0 +1,77 @@ +#include "ok.h" + +int chargIutDon(VilleIut *tVilleIut[], int nbMax, char nomFich[]) +{ + FILE *flot; + int i=0, nbP, trouve, indice; + char nom[30], nomDept[30], resp[30]; + + flot = fopen(nomFich, "r"); + if(flot==NULL) + { + printf("Probleme d'ouverture du fichier\n"); + exit(1); + } + fscanf(flot, "%s", nom); + lireDep(flot, nomDept, &nbP, resp); + while(!feof(flot)) + { + if(i==nbMax) + { + printf("Tableau plein\n"); + return -1; + } + indice = appartientIut(tVilleIut, i, nom, &trouve); + if(trouve==0) + { + tVilleIut[i] = (VilleIut*)malloc(sizeof(VilleIut)); + if(tVilleIut[i]==NULL) + { + printf("Probleme malloc\n"); + fclose(flot); + exit(1); + } + strcpy(tVilleIut[i]->nom, nom); + tVilleIut[i]->ldept = listenouv(); + ajouterDept(tVilleIut[i]->ldept, nomDept, resp, nbP); + } + if(trouve==1) + ajouterDept(tVilleIut[indice]->ldept, nomDept, resp, nbP); + i = i + 1; + fscanf(flot, "%s", nom); + lireDep(flot, nomDept, &nbP, resp); + } + return i; +} + +void lireDep(FILE *flot, char nomDept[], int *nbP, char resp[]) +{ + fscanf(flot,"%s%d\t", nomDept, nbP); + fgets(resp, 30, flot); + + #ifdef _WIN32 + resp[strlen(resp) - 1] = '\0'; + #endif + + #ifdef __linux__ + resp[strlen(resp) - 2] = '\0'; + #endif +} + +int appartientIut(VilleIut *tVilleIut[], int nb, char nom[], int *trouve) +{ + int i = 0; + + while(i < nb) + { + if(strcmp(tVilleIut[i]->nom, nom) == 0) + { + *trouve = 1; + return i; + } + i = i + 1; + } + *trouve = 0; + return i; +} + From 87baece1308fa363230ce3329fe4092805d084da Mon Sep 17 00:00:00 2001 From: d3friski Date: Fri, 6 Jan 2023 14:17:42 +0100 Subject: [PATCH 2/2] Push pour correc include et ajout dans sae.h --- header/sae.h | 6 ++++++ source/chargEtSauvFich.c | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/header/sae.h b/header/sae.h index 6698d13..1ed0b26 100644 --- a/header/sae.h +++ b/header/sae.h @@ -119,3 +119,9 @@ int longueur(ListeDept ldept); void afficherListe(ListeDept ldept); // iut.c + + +// chargEtSauvFich.c +int chargIutDon(VilleIut *tVilleIut[], int nbMax, char nomFich[]); +void lireDep(FILE *flot, char nomDept[], int *nbP, char resp[]); +int appartientIut(VilleIut *tVilleIut[], int nb, char nom[], int *trouve); diff --git a/source/chargEtSauvFich.c b/source/chargEtSauvFich.c index 74ba2cd..be73c61 100644 --- a/source/chargEtSauvFich.c +++ b/source/chargEtSauvFich.c @@ -1,4 +1,4 @@ -#include "ok.h" +#include "sae.h" int chargIutDon(VilleIut *tVilleIut[], int nbMax, char nomFich[]) {