Pass data from txt to struct C

1

Very good guys, I have a dudilla and I do not know how to raise it, I have to import a .txt data file into a structure. The structure is this:

  

Structure:

typedef struct
{
    char nomEditorial[TAM];
    int numPagina;

} tRegLibros;

typedef struct
{
    char nomRevista[TAM];
    int numRevista;
    int paginaInicio;

} tRegArticulos;

typedef struct
{
    char titulo[TAM];
    char autor[TAM];
    char idEjemplar[5];
    int anioPubli;
    int prestado;
    char fecha[20];
    char fechaDev[20];
    char dniSocio[9];
    union
        uTipo
    {
        tRegArticulos articulo;
        tRegLibros libro;
    } uTipo;
} tRegEjemplares;

typedef struct
{
    tRegEjemplares ejemplar[maxEJEMPLARES];
    int contador;
    int contadorLibros;
    int contadorArticulo;
} tRegLista;

And the txt data goes like this: * Info in the images

Then I had raised something like that, but I do not know how to approach the problem to see if you can clarify me.

void Importar(tRegLista *reg)
{
    char ruta[TAM];
    tRegLista aux;

    system("cls");
    printf("*******************************************************************************\n");
    printf("                                Importar Ejemplares\n");
    printf("*******************************************************************************\n\n\n");
    printf("Introduce la ruta del archivo a importar:\n");
    fflush(stdin);
    gets(ruta);

    FILE *fichero;
    fichero=fopen(ruta,"r");

    if(fichero == NULL)
     {
     printf("\nERROR: No existe ese fichero\n");
     exit(EXIT_FAILURE);
     }





    fclose(fichero);
}

I add information in the file for example 1 book 1 article one below the other followed and without the title in plan TITLE:, YEAR: the data go directly.

    
asked by Peter 25.04.2017 в 19:28
source

1 answer

1

Use the fgets function to read the file line by line and save the data in the corresponding fields of your auxiliary structure and then compare them with the main struct

fgets(aux->'campo donde lo vayas a guardar' , TAM , fichero);

The fgets reads a whole line and at the end of the variable where you are saving it adds a \ n, to remove it do the following:

aux->titulo[strlen(aux->titulo) - 1)] = 0;

This way you will get the \ n out of the last position of your chain

Greetings and encouragement with the practice xDD:))

    
answered by 28.04.2017 / 20:00
source