I have a problem when I try to read the file information for my struct with fscanf()
. It just reads the first line of the string and the loop never ends. How can I solve the problem?
Struct
typedef struct {
int id;
char name[80];
char nameStadium[80];
int numberPlacesStadium;
float funds;
float monthlyExpenses;
int active;
} Team;
And I use this code to read
void showAll(void)
{
FILE* file;
Team team;
file = fopen("file.txt", "rt");
if (file == NULL)
{
printf("!!!Cant open file!!!\n");
return;
}
rewind(file);
printf("\n\n=== TEAMS ======\n");
printf("%s\t%s\n", "ID", "NAME");
while (fscanf(file, "%6d %s %s %6d %f %f %03d\n", &team.id, team.name, team.nameStadium, &team.numberPlacesStadium, &team.funds, &team.monthlyExpenses, &team.active) != EOF)
{
if (team.active != 0)
{
printf("%d\t%s\n", team.id, team.name);
}
}
fclose(file);
}
I can't understand why fscanf()
just gets the first word and not the full string
Does anyone know how to solve this?