Hello good results that I have a problem with an exercise, the enunciation is the following:
-Write a program that reads a file created called "personas.txt".
This file contains data from three people: (people.txt) ·First name ·Surname · NIF ·Age · Height
It has to show all the data of the people by screen, if those people are 18 years old or more:
Here the text of the file: (people.txt)
Juan
Antonio
123456789
18
1.98
Manolo
Escobar
987654321
76
1.59
Cristiano
Ronaldo
542312345
31
1.86
Here I pass the code of my program that fails me:
int llegirMajorsEdat()
{
int i;
int edat;
float altura;
int NIF;
char *nom;
char *cognom;
char linea[1024];
FILE * file;
file=fopen("persones.txt", "r");
for(i=0; i<3; i++ )
{
fgets(linea, 1024, file);
fgets(linea, 1024, file);
fgets(linea, 1024, file);
nom = strtok(linea, " ");
fgets(linea, 1024, file);
cognom = strtok(linea, " ");
fgets(linea, 1024, file);
NIF = atoi(strtok(linea, " "));
fgets(linea, 1024, file);
edat = atoi(strtok(linea, " "));
printf("%d\n", edat );
fseek(file, 0, SEEK_CUR);
altura = atof(fgets(linea, 11, file));
if (edat >= 18)
{
printf("--------------------------\n");
printf("Aquesta persona és major d'edat\n");
printf("%s", nom);
printf("%s", cognom);
printf("%d\n", NIF );
printf("%d\n", edat );
printf("%.2f\n", altura );
printf("--------------------------\n");
}
}
}
int main()
{
char linea[1024];
char persones[25];
int i;
int j;
FILE * file;
char * token;
file = fopen("persones.txt", "r");
llegirMajorsEdat();
return 0;
}