I am trying to create a function which with read from a file some grades with the student names and print the grades in decreasing order. This is the function:
char studenteUN[50];
int punti;
int maxP = MAX_LNG;
int max = -1000;
int min = MAX_LNG;
FILE *ftemp = f;
while(fscanf(ftemp, "%s", studenteUN) != EOF){
fscanf(ftemp, "%d", &punti);
if(punti < min)
min = punti;
}
while (maxP != min) {
max = -1000;
ftemp = muovi(f);
while(fscanf(ftemp, "%s", studenteUN) != EOF){
fscanf(ftemp, "%d", &punti);
if(punti > max && punti < maxP)
max = punti;
}
maxP = max;
ftemp = muovi(f);
while(fscanf(ftemp, "%s", studenteUN) != EOF){
fscanf(ftemp, "%d", &punti);
if(punti == max)
printf("%s %d\n", studenteUN, punti);
}
}
Te output is the following:
asdas 8 asdas 7 asdsa 6 andrea 5 asd 4 asd 1 asdsad 1 asdas 0 asd 0
I can not figure it out why this happens. The file I read from is the following:
2
1- askd a
1- asd
2- asd as
3- asd as d
4- asdas
5- Non lo so.
1
2- asdsad asd as d
1- asd a
2- asd
3- asd
4- as df
5- Non lo so.
2
andrea 5
asdsa 6
asdas 7
asd 1
asdas 0
asd 0
asdsad 1
asdas 8
asd 4
The muovi function:
FILE *muovi(FILE *f){
fseek(f, 0, SEEK_SET);
char resultato[MAX_LNG];
int nD;
fscanf(f, "%d", &nD);
printf("\n");
for(int i = 0; i <= nD*7;i++)
fgets(resultato, MAX_LNG, f);
return f;
}