打印后文件中的C链接列表未退出

时间:2016-05-24 02:08:58

标签: c file linked-list

所以,我必须从文件输入中打印一个链表,我已设法使用它:

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

typedef struct Vehicle{
    int option;
    char make [30];
    char model[30]; 
    int car_manufacture_date;
    float maximum_velocity;
    float mass;
    int seats;
    struct Vehicle *next;//linked list node
} vehicle_t;


int main (){

    FILE* fp;
    fp = fopen("vehicles.crash.txt", "r");

    vehicle_t* first_car = malloc(sizeof(vehicle_t));
    if (first_car == NULL){
        printf("Error. Failed to allocate memory to first_car\n");
        exit(1);
    }
    vehicle_t* current_vehicle = malloc(sizeof(vehicle_t));
    if (current_vehicle == NULL){
        printf("Error. Failed to allocate memory to current_vehicle\n");
        exit(1);
    }
    vehicle_t* new_vehicle = malloc(sizeof(vehicle_t));
    if (new_vehicle == NULL){
        printf("Error. Failed to allocate memory to new_vehicle\n");
        exit(1);
    }
    printf("GOOD1\n");

    current_vehicle = first_car;
    new_vehicle = first_car;
    printf("GOOD2\n");

    //Loading vehicles from file to linked list
    if (fp != NULL)
    {
        printf("GOOD3\n");
        while (fscanf(fp,"%d %s %s %d %f %f %d", &new_vehicle->option, new_vehicle->make, new_vehicle->model, &new_vehicle->car_manufacture_date, 
        &new_vehicle->maximum_velocity, &new_vehicle->mass, &new_vehicle->seats) != EOF)
        {
            printf("GOOD4\n");
            current_vehicle->next = new_vehicle;
            current_vehicle = current_vehicle->next;
            new_vehicle = malloc(sizeof(vehicle_t));
            if (first_car == NULL){
                printf("Error. Failed to allocate memory\n");
                new_vehicle->next=NULL;
                exit(1);
            }

            printf("GOOD5\n");
        }
    close(fp);
    printf("Input completed\n");
    }
    else
        printf("Error! couldn't find file\n");

    current_vehicle = first_car;

    while (current_vehicle != NULL) 
    {
        printf("Option: %d\tMake: %s\tModel: %s\tManufactured: %d\tMax Velocity: %.2f\tMass: %.2f\tSeats: %d\n",
        current_vehicle->option, current_vehicle->make, current_vehicle->model, current_vehicle->car_manufacture_date, 
        current_vehicle->maximum_velocity, current_vehicle->mass, current_vehicle->seats);

        new_vehicle = current_vehicle->next;
        current_vehicle = current_vehicle->next;
    };
    printf("Printing completed");
return 0;
}

一切正常,直到最后一个文件项被打印出来,然后程序崩溃。从我在其他帖子中看到的情况来看,while循环将它们全部匹配。

打印的"GOOD"语句只是检查点

文件中的文字格式为:1 Toyota Camry 2010 200.0 1100.0 5

2 个答案:

答案 0 :(得分:0)

我认为您忘记将最后一个列表节点的->next字段设置为NULL。您刚刚调用malloc,因此该值可能是随机的,使您的打印失败。

答案 1 :(得分:0)

无限循环的主要原因是未能将最终next设置为NULL [正如其他人提到的那样]。

但是,我认为使用简化的代码更容易看到,虽然大多数情况下都是正确的,但却比它需要的更复杂。

无论如何,这个编译但我没有测试它[请原谅无偿的风格清理]:

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

typedef struct Vehicle {
    int option;
    char make[30];
    char model[30];
    int car_manufacture_date;
    float maximum_velocity;
    float mass;
    int seats;
    struct Vehicle *next;               // linked list node
} vehicle_t;

int
main()
{
    FILE *fp;

    fp = fopen("vehicles.crash.txt", "r");
    if (fp == NULL) {
        printf("Error! couldn't find file\n");
        exit(1);
    }

    vehicle_t *first_car = NULL;
    vehicle_t *previous_vehicle = NULL;

    // Loading vehicles from file to linked list
    while (1) {
        vehicle_t *new_vehicle = malloc(sizeof(vehicle_t));

        if (new_vehicle == NULL) {
            printf("Error. Failed to allocate memory to new_vehicle\n");
            exit(1);
        }

        // NOTE: the lack of this [in your equivalent code] was the reason
        // for the infinite loop
        new_vehicle->next = NULL;

        if (fscanf(fp, "%d %s %s %d %f %f %d",
            &new_vehicle->option, new_vehicle->make, new_vehicle->model,
            &new_vehicle->car_manufacture_date, &new_vehicle->maximum_velocity,
            &new_vehicle->mass, &new_vehicle->seats) == EOF) {
                free(new_vehicle);
                break;
        }

        if (first_car == NULL)
            first_car = new_vehicle;
        else
            previous_vehicle->next = new_vehicle;

        previous_vehicle = new_vehicle;
    }

    fclose(fp);

    vehicle_t *current_vehicle = first_car;

    while (current_vehicle != NULL) {
        printf("Option: %d\tMake: %s\tModel: %s\tManufactured: %d\tMax Velocity: %.2f\tMass: %.2f\tSeats: %d\n",
            current_vehicle->option, current_vehicle->make,
            current_vehicle->model, current_vehicle->car_manufacture_date,
            current_vehicle->maximum_velocity, current_vehicle->mass,
            current_vehicle->seats);
        current_vehicle = current_vehicle->next;
    };

    printf("Printing completed");

    return 0;
}