Problem with a C exercise

1

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;
}
    
asked by GokuGod97 28.05.2016 в 13:17
source

1 answer

1

Here I go, the reason why you can not get the result you are looking for is because when using fgets(<>,<>,<>); you are getting the whole line and you have results by lines, the most advisable in this case is to use a delimited file, since Space ("") is not a good friend when it comes to tokenize a string.

This is the "People.txt" file that I used:

Juan,Antonio,123456789,18,1.98
Manolo,Escobar,987654321,76,1.59
Cristiano,Ronaldo,542312345,31,1.86
Miguel,Gomez,123432123,15,1.77

The idea is to get the adult, right? Then I show you what I did to achieve it:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int llegirMajorsEdat(FILE *file)
{
    int i;
    int edat;
    float altura;
    char *NIF;
    char *nom;
    char *cognom;
    char linea[1024];

    for(i = 0; i < 3; i++)
    {
        fgets(linea, 1024, file);
        /* Imprime la linea actual.*/ puts(linea);
        nom = strtok(linea, ",");
        cognom = strtok(NULL, ",");
        NIF = strtok(NULL, ",");
        edat = atoi(strtok(NULL, ","));
        altura = atof(strtok(NULL, ","));

        if (edat >= 18) 
        {
            printf("--------------------------\n");
            printf("Aquesta persona és major d'edat\n");

            printf("%s ", nom);
            printf("%s\n", cognom);
            printf("%s \n", NIF );
            printf("%d\n", edat );
            printf("%.2f\n", altura );
            printf("--------------------------\n");
        }
    }
    return 1; // Sin esto no compilaba (strict)
}

int main()
{
    char linea[1024];
    char persones[25];
    int i;
    int j;

    FILE * file;
    char * token;
    file = fopen("archis.txt", "r");

    llegirMajorsEdat(file);
    return 0;
}

It's simple, first take the first line to be delimited, then call strtok for the first time in the variable nom , then define the current line for each field until you reach the last one (\ n) and then pass to the next cycle of the for loop.

Finally, it is verified that he is of legal age and the details are printed.

  

You have seen, that I have added int llegirMajorsEdat() a parameter called FILE *file , this is so that you do not have to open the same file twice when calling it from the function main() , but you pass it by parameter and ready !

If you are interested in the result that you give me when you try it:
--------------------------
Aquesta persona Ts major d'edat
Juan Antonio
123456789
18
1.98
--------------------------
--------------------------
Aquesta persona Ts major d'edat
Manolo Escobar
987654321
76
1.59
--------------------------
--------------------------
Aquesta persona Ts major d'edat
Cristiano Ronaldo
542312345
31
1.86
--------------------------

I hope it helped you.

    
answered by 28.05.2016 / 20:51
source