繁体   English   中英

C 将数据从txt文件加载到链表中

[英]C loading data from txt file into a linked list

我想制作一个程序,从 txt 文件加载数据并将其放入链接列表,然后打印该链接列表。 它似乎有效,但它也会打印出一些垃圾。 我对链表很陌生,所以这可能是一些明显的错误。 如果有人告诉我如何解决它,我将不胜感激,在此先感谢。

这就是我的 txt 文件中的内容:

FORD    FIESTA  2010    1.6
OPEL    ASTRA   2005    1.4

这就是我的程序打印出来的:

FORD    FIESTA  2010    1.6
OPEL    ASTRA   2005    1.4
­iŁ     ;C:\Windows;C:\Windows\System32\      1251983754      132.41

这是我的代码:

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

typedef struct Cars
{
    char brand[30], model[30];
    int year;
    float engine;
    struct Cars *next;
} Cars_t;

void load_cars(Cars_t *head, char  file_name[30])
{
    Cars_t *temp = head;

    FILE *baza = fopen(file_name, "r");
    if(baza!=NULL)
    {
        while(fscanf(baza, "%s%s%d%f", temp->brand, temp->model, &(temp->year), &(temp->engine)) == 4)
        {
                temp->next=malloc(sizeof(Cars_t));
                temp=temp->next;
        }
        temp->next=NULL;
    }
    fclose(baza);
}

void print_list(Cars_t *head)
{
    Cars_t *current = head;

    while (current != NULL)
    {
        printf("%s\t%s\t%d\t%.2f\n", current->brand, current->model, current->year, current->engine);
        current = current->next;
    }
}

int main()
{
    Cars_t *head = malloc(sizeof(Cars_t));
    load_cars(head, "baza.txt");
    print_list(head);

    return 0;
}

您阅读了 2 个结构并分配了 3 次,首先是 main 中的 head。 然后当你成功地从文件中读取两次。 所以你最后的分配没有意义。 我还为您的代码添加了一些安全检查,但您只需要注意带有// <---- Special atention here!

顺便说一句,这个例子不是最好的方法,我只是做了一个快速修复,让你了解问题出在哪里。

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

typedef struct Cars Cars_t, *pCars_t;
struct Cars
{
    char brand[30], model[30];
    int year;
    float engine;

    pCars_t next;
};

void load_cars(Cars_t *head, char  file_name[30])
{
    Cars_t *temp = head;
    Cars_t *previous = NULL; // <---- Special atention here!

    FILE *baza = fopen(file_name, "r");
    if(baza!=NULL)
    {
        while(fscanf(baza, "%s%s%d%f", temp->brand, temp->model, &(temp->year), &(temp->engine)) == 4)
        {   
            temp->next=malloc(sizeof(Cars_t));
            if(temp->next == NULL)
            {
                fprintf(stderr, "Allocation error!\n");
                exit(1);
            }
            previous = temp;   // <---- Special atention here!
            temp=temp->next;
        }
        temp->next=NULL;
        free(previous->next);  // <---- Special atention here!
        previous->next=NULL;   // <---- Special atention here!

    }
    else
    {
        fprintf(stderr, "File problem!");
        return;
    }
    fclose(baza);
}

void print_list(Cars_t *head)
{
    Cars_t *current = head;

    while (current != NULL)
    {
        printf("%s\t%s\t%d\t%.2f\n", current->brand, current->model, current->year, current->engine);
        current = current->next;
    }
}

int main()
{
    pCars_t head = malloc(sizeof(Cars_t));
    if(head == NULL)
    {
        fprintf(stderr, "Allocation error!\n");
        return 1;
    }
    load_cars(head, "baza.txt");
    print_list(head);

    return 0;
}

暂无
暂无

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

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