简体   繁体   English

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

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

I'm new to C programming, and I'm having some difficulties.我是 C 编程的新手,我遇到了一些困难。 I'm trying to re-code to use a linked list to store the structs you read from the file, but I can't understand how to save the text line for line and add it to the linked list.我正在尝试重新编码以使用链表来存储您从文件中读取的结构,但我不明白如何保存文本行并将其添加到链表中。

This is my code:这是我的代码:

#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;
}

Struct code:结构代码:

#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

If anyone could give me some pointers on what I need to do next to make it work, I would highly appreciate it.如果有人能给我一些关于我下一步需要做什么以使其工作的指示,我将不胜感激。 Thank you谢谢

You could implement a linked list of ' Car ' objects by adding a next pointer to the Car structure.您可以通过添加指向Car结构的next指针来实现“ Car ”对象的链表。 For example:例如:

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;

(An assumption is made about Car as source code for Cars.h was not provided.) (假设Car的源代码Cars.h提供。)

You would have to account for this extra field in Cars.dat (where it could be stored with a NULL value assuming that the file has just an array and no notion of a linked list).您必须考虑Cars.dat中的这个额外字段(假设文件只有一个数组并且没有链表的概念,它可以与NULL值一起存储)。

When reading, you could malloc space for each Car separately, fread it into the malloc -ed buffer, and update the previous ' Car 's next pointer to point to the newly loaded Car .读取时,您可以为每个Car分别提供malloc空间,将其freadmalloc的缓冲区中,并更新前一个' Carnext指针以指向新加载的Car In this manner you can build up a simple linked list of Car s.通过这种方式,您可以建立一个简单的Car链表。

ps I've never heard of a Toyota Civic:) ps 我从没听说过丰田思域:)

You can create a new struct as below which includes your Car struct as a field.您可以创建一个新结构,如下所示,其中包含您的Car结构作为字段。 And a pointer to the next node.以及指向下一个节点的指针。

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

And you don't need to calculate the count of records in the file.而且您不需要计算文件中的记录count Just read one and malloc a CarNode and use the CarNode.payload field as the buffer to hold the content read from the file.只需读取一个和malloc一个CarNode并使用CarNode.payload字段作为缓冲区来保存从文件中读取的内容。 Something like below snippet:类似于下面的片段:

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