繁体   English   中英

(从文件中读取)C 中的链表

[英](Read from file) Linked list in C

我是 C 编程的新手,我遇到了一些困难。 我正在尝试重新编码以使用链表来存储您从文件中读取的结构,但我不明白如何保存文本行并将其添加到链表中。

这是我的代码:

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


void write_to_file() {
    FILE* file = NULL;
    file = fopen("Cars.dat", "wb");
    Car c = { "Toyota", "Civic", "black", 5, 50000 };
    size_t ret = fwrite(&c, sizeof c, 1, file);
    Car d = { "Toyota1", "Civic1", "Red", 2, 55000 };
    ret = fwrite(&d, sizeof d, 1, file);
    fclose(file);

}

void print_cars(Car *c, int count) {
    FILE* f = fopen("output.txt", "w");
    for (int i = 0; i < count; i++) {
        fprintf(f, "%s %s %s %d %f\n", c[i].model, c[i].manufacturer, c[i].color, c[i].seatCapacity, c[i].price);
    }
    fclose(f);
}


void read_from_file() {
    Car c;
    size_t SIZE = sizeof c;
    size_t ret = 1;
    FILE* file = NULL;
    file = fopen("Cars.dat", "rb");
    int count = 0;
    do{
        ret = fread(&c,  sizeof c, 1, file);
        if (ret != 1)
            break;
        count++;
        //printf("%s %s %s %d %f\n", c.model, c.manufacturer, c.color, c.seatCapacity, c.price);
    }while(!feof(file));
    fclose(file);
    if (count == 0) {
        printf("No cars in the file provided\n");
        return;
    }
    file = fopen("Cars.dat", "rb");
    Car* cars_array = NULL;
    cars_array = (Car *)malloc(count * SIZE);
    ret = fread(cars_array, SIZE*count, 1, file);
    //printf("%d\n", ret);
    print_cars(cars_array, count);

}

int main()
{
    //write_to_file();
    read_from_file();
    return 0;
}

结构代码:

#ifndef Cars_h
#define Cars_h

typedef struct cars {
    char manufacturer[35]; //toyota, honda
    char model[35]; //camry, civic
    char color[20]; //black, red
    int seatCapacity; //4,5
    float price; //20k, 25k 
}
Car;

#endif

如果有人能给我一些关于我下一步需要做什么以使其工作的指示,我将不胜感激。 谢谢

您可以通过添加指向Car结构的next指针来实现“ Car ”对象的链表。 例如:

typedef struct Car {
    /* existing fields */
    char model[LLL];
    char manufacturer[MMM];
    char color[CCC];
    int seatCapacity;
    float price;

    struct Car *next; /* new member for linked list */
} Car;

(假设Car的源代码Cars.h提供。)

您必须考虑Cars.dat中的这个额外字段(假设文件只有一个数组并且没有链表的概念,它可以与NULL值一起存储)。

读取时,您可以为每个Car分别提供malloc空间,将其freadmalloc的缓冲区中,并更新前一个' Carnext指针以指向新加载的Car 通过这种方式,您可以建立一个简单的Car链表。

ps 我从没听说过丰田思域:)

您可以创建一个新结构,如下所示,其中包含您的Car结构作为字段。 以及指向下一个节点的指针。

typedef struct {
    Car payload;
    CarNode* nextNode;
}CarNode

而且您不需要计算文件中的记录count 只需读取一个和malloc一个CarNode并使用CarNode.payload字段作为缓冲区来保存从文件中读取的内容。 类似于下面的片段:

CarNode head, tail; // Suppose you are using single linked list.
/*
Initialize the head and tail pointer. At first, they should both point to the first node.
*/
...

/*
Allocate a new node while reading a record from the file.
*/
CarNode *p = (CarNode *)malloc(sizeof(CarNode));
ret = fread(&(p->payload), sizeof(Car), 1, file);

/*
Insert the newly read node into the link list.
*/
tail->nextNode = p;
tail = p;

暂无
暂无

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

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