Ich versuche, eine Datei zu lesen und die Datensätze zu drucken, wo der Jumper die vom Benutzer angegebene Entfernung überschritten hat, aber die Suche gibt nur die erste Zeile in der Datei zurück und das war es, Ich weiß, dass in der Logik etwas nicht stimmt, aber ich kann nicht darauf hinweisen.Suche nur das erste Ergebnis in Struktur C
#include<stdio.h>
#include<string.h>
typedef struct {
char fname[30];
char lname[30];
char nationality [30];
double distance;
}Player;
void print(Player p) {
printf("%s %s %s %f \n", p.fname, p.lname, p.nationality, p.distance);
}
int search(double distance, Player allPlayers[], int max){
int i=0;
for(i=0; i<max; i++){
if(distance > allPlayers[i].distance){
return i;
}
return -1;
}
}
void load (char filename[10], Player players[]){
char tempfName [30];
char templName [30];
char tempNationality [30];
double tmpdistance;
FILE * input=fopen(filename,"r+");
if(input==NULL)
printf("Error found in opening the file\n");
else {
printf("The data are loaded successfully.\n");
int counter = 0;
while(!feof(input)){
strcpy(tempfName," ");
fscanf(input, "%s %s %s %f",tempfName, templName, tempNationality, &tmpdistance);
if(strcmp(tempfName, " ") !=0){
strcpy(players[counter].fname, tempfName);
strcpy(players[counter].lname, templName);
strcpy(players[counter].nationality, tempNationality);
players[counter].distance = tmpdistance;
counter++;
}
}
fclose(input);
}
}
int main (int argc, char *argv[]){
Player players2[40];
char myFileName[10] ="jump.txt";
load(myFileName, players2);
double tmpdistance;
printf("Please enter the distance threshold: ");
scanf("%lf", &tmpdistance);
printf("The jumpers exceeded 8.10 m are: \n");
int index = -1;
index = search(tmpdistance,players2,40);
if(index!=-1)
print(players2[index]);
else
printf("NOT Found! \n");
return 0;
}
einige Beispieldaten aus der Datei zu lesen Ich versuche:
Arsen Sargsyan Armenien 7,62
Boleslav Skhirtladze Georgia 7,26
Christian Reif Deutschland 7.92
Christopher Tomlinson Great_Britain 8,06
'return -1;' nach for for-loop verschieben. – BLUEPIXY
Verwenden Sie '% lf' anstelle von'% f' von 'fscanf' für' double' – BLUEPIXY