【问题标题】:(Read from file) Linked list in C(从文件中读取)C 中的链表
【发布时间】:2020-04-13 03:19:26
【问题描述】:

我是 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

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

【问题讨论】:

标签: c


【解决方案1】:

您可以通过将next 指针添加到Car 结构来实现“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 值一起存储)。

阅读的时候,你可以把malloc的空间分别给每个Carfread放到malloc-ed缓冲区中,并更新previous'Car的@ 987654334@ 指向新加载的Car 的指针。通过这种方式,您可以构建一个简单的Cars 链表。

附言我从未听说过丰田思域 :)

【讨论】:

  • Cars.h 源代码是#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 不知道怎么把代码放在评论区
  • @JorgeMorales 您可以将其粘贴到问题中。使用问题下方的edit 链接。
【解决方案2】:

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

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

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

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;

【讨论】:

    猜你喜欢
    • 2014-11-02
    • 2018-06-02
    • 1970-01-01
    • 2019-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    • 2017-05-03
    相关资源
    最近更新 更多