簡體   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