简体   繁体   English

如何从文本文件中读取格式化的行并将信息保存在链接列表中? IN C

[英]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; char * name int age char *国家/地区;

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. 我的意思是,就像char *被覆盖之类的东西一样,我不知道问题出在哪里。

In *agregar(nodo *n, char *buffer) change this: *agregar(nodo *n, char *buffer)更改以下内容:

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. 指针lineaAux将指向最后读取的内容,因此每个节点的texto ptr也将指向该内容。 Copy the contents of lineaAux to text. 将lineaAux的内容复制到文本。

You also have a problem with dato. 您也对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): 还要重新考虑如何读取文件并检查EOF,并考虑这样的事情是否更好(假设文件中没有空行-在对可能不存在的数据盲目使用strtok之前应该检查一下):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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