I'm trying to create 4 arrays with a text file. The text file look like this :
1000 Docteur Albert 65.5
1001 Solo Hanz 23.4
1002 Caillou Frederic 78.7
…
The code:
void creer (int num[], char pre[][TAILLE_NP+1], char nom[][TAILLE_NP+1],
float note[], int * nb ){
int n = 0, i; /*nb personnes*/
FILE *donnees = fopen("notes.txt", "r");
if(!donnees){
printf("Erreur ouverture de fichier\n");
exit(0);
}
while (!feof(donnees)){
fscanf(donnees,"%d", &num [n]);
fgets(nom[n], TAILLE_NP+1, donnees);
fgets(pre[n], TAILLE_NP+1, donnees);
fscanf(donnees,"%f\n", ¬e[n]);
printf("%d %s %s %f\n",num[n], nom[n], pre[n], note[n]);
n++;
}
fclose (donnees);
*nb = n ;
}
int main() {
int num[MAX_NUM];
int nbEle;
char pre[MAX_NUM][TAILLE_NP+1],
nom[MAX_NUM][TAILLE_NP+1];
float note[MAX_NUM];
creer (num, pre, nom, note, &nbEle);
printf("%s", pre[2]); //test
return 0;
}
The problem is, I'm sure there is a better way to create the arrays, im a beginner. Also, there is a problem with the float, the decimal aren't right when I printf. For example, 78.7 become 78.699997. What am I doing wrong? Thanks ! :)