简体   繁体   中英

How to read formatted lines from a text file and save info in a linked list? IN C

I have a code where I'm working with a linked list using pointers (actually it's more like a stack, 'cause it adds nodes on the top). Now I need to read a text file with lines like this:

NAME, AGE, COUNTRY
NAME2, AGE2, COUNTRY2

and so on... and then, assign those three values from each line, to three different variables, so I can assign those values to each node. Something like this:

char *name int age char *country;

I don't know the size of each char, that's why I can't use arrays. Sorry if I'm not explaining this very good, but english is not my first language. Anyway, here's what I have:

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

typedef struct {
    // informacion contenida en el nodo
    int dato;
    int dato2;
    char *texto;
    // propiedades del nodo
    struct nodo *siguiente;
} nodo;

nodo *crearNodo(){
    nodo *L = malloc(sizeof(nodo));
    L->siguiente = NULL;
    return L;
}

nodo *agregar(nodo *n, char *buffer){
    // creo nodo temporal y le asigno espacio
    nodo *temp;
    char *lineaAux;
    temp = (nodo *)malloc(sizeof(nodo));

    // hago que el nodo temporal apunte a lo mismo que la cabecera
    temp->siguiente = n->siguiente;
    // le asigno datos al nodo temporal
    lineaAux = strtok(buffer, ",");
    temp->dato = atoi(lineaAux);

    lineaAux = strtok(NULL, ",");
    temp->dato2 = atoi(lineaAux);

    temp->texto = (char *)malloc(30*sizeof(char));
    lineaAux = strtok(NULL, "\n");
    temp->texto = lineaAux;
    // hago que la cabecera sea el nodo temporal, es decir, lo inserto adelante. Empujo.
    n->siguiente = temp;
    return n;
}

nodo *cargar(nodo *votantes, char *archivo){
    FILE *txt = fopen(archivo, "r");

    if(txt == NULL){
        printf("chupalo");
    } else {
        char *buffer = (char *)malloc(512*sizeof(char));
        while(!feof(txt)){
            buffer = fgets(buffer, 512, txt);
            votantes = agregar(votantes, buffer);
        }
    }
    fclose(txt);
    return votantes;
}

void mostrar(nodo *n){
    nodo *aux;
    if(n->siguiente == NULL){
        printf("Lista vacia ");
    } else {
        aux = n->siguiente;
        do {
            printf("dato1: %d dato2: %d dato3: %s\n", aux->dato, aux->dato2, aux->texto);
            aux = aux->siguiente;
        }
        while(aux != NULL);
    }
}

main(){
    nodo *listaEnlazada = crearNodo();
    char *txt = "lista.txt";
    listaEnlazada = cargar(listaEnlazada, txt);
    mostrar(listaEnlazada);
    return 0;
}

EDIT:-------------------

ahahaha oh right, I didn't say it. Well, if I have this in the text files:

1, word
2, word2
3, word3

My program prints

num: 3 - word: word3
num: 2 - word: word3
num: 1 - word: word3

I mean, it's like the char* it's overwritten or something, I don't know where the problem is.

In *agregar(nodo *n, char *buffer) change this:

temp->texto = lineaAux;

to

strncpy(temp->texto, lineaAux, 30);

The pointer lineaAux is going to point to whatever is the last thing read and consequently so will every node's texto ptr. Copy the contents of lineaAux to text.

You also have a problem with dato. From the file it looks like it should be a string (it is a name, right?) but you are treating it like an integer.

Also re-think how you are reading the file and checking for EOF and consider if something like this is better (assuming no blank lines in the file - which you should check for before blindly using strtok on data that may not be present):

    while ((buffer = fgets(buffer, 512, txt)) != NULL)
    {
        votantes = agregar(votantes, buffer);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM