简体   繁体   English

从文件中读取链表并加载数据

[英]Read linked list from a file and load the data

I wrote a C code to implement a dictionary using linked-list(nodes are in sorted order) I want to save the data to a file and be able to reload the data when I run the program next time. 我编写了一个C代码以使用链表实现字典(节点按排序顺序),我想将数据保存到文件中,并在下次运行程序时能够重新加载数据。 I'm having trouble loading the data from file. 我无法从文件加载数据。 Here is my code for reading and writing data: 这是我用于读取和写入数据的代码:

struct node{
    char word[20];
    char meaning[5][100]; //2D array for saving multiple meanings of a word
    struct node *next;
};

void ReadData(struct node *head)
{
    struct node *tail;
    FILE *fp = fopen("dictionary.data", "rb");
    if(fp == NULL)
    {
        printf("Error opening file..\n");
        return;
    }
    while(!feof(fp))
    {
        tail = (struct node*)calloc(1, sizeof(struct node));
        fread(tail->word, sizeof(head->word), 1, fp);
        fread(tail->meaning, sizeof(head->meaning), 1, fp);
        if(head == NULL) //for fresh run head is initialized with NULL
        {
            tail->next = head;
            head = tail;
        }
        else
        {
            head->next = tail;
            head = head->next;
        }
    }
    fclose(fp);
}

I can't load data from the file to the linked list. 我无法将数据从文件加载到链接列表。 Code is not working. 代码不起作用。 I can't figure out where the problem is.. 我不知道问题出在哪里。
Here's how I'm writing the data to the file : 这是我将数据写入文件的方式:

/*I think this code is working because size of the file increases after running the code*/
void WriteData(struct node *head)
{
    FILE *fp = fopen("dictionary.data", "wb");
    if(fp == NULL)
    {
        printf("Error opening file..\n");
        return;
    }
    while(head != NULL)
    {
        fwrite(head->word, sizeof(head->word), 1, fp);
        fwrite(head->meaning, sizeof(head->meaning), 1, fp);
        head = head->next;
    }
    fclose(fp);
}

I've used sizeof , instead of strlen , it's a string. 我用过sizeof ,而不是strlen ,它是一个字符串。 It'll have null character at the end - no problem with string. 它的结尾将为空字符-字符串没有问题。 It'll consume more memory though. 它会消耗更多的内存。

try this (untested): 试试这个(未试用):

void ReadData(struct node **head){//or struct node *ReadData(void){ ... return head; }
    struct node temp = { .next = NULL };//{ {0}, {{0}}, NULL}
    struct node *hp, *curr;
    FILE *fp = fopen("dictionary.data", "rb");

    if(fp == NULL){
        printf("Error opening file..\n");
        return;
    }

    hp = *head;
    while(fread(temp.word, sizeof(temp.word), 1, fp) && fread(temp.meaning, sizeof(temp.meaning), 1, fp)){
        struct node *np = malloc(sizeof(*np));
        if(np == NULL){
            perror("couldn't make new node by malloc:");
            return ;//release list
        }
        *np = temp;

        if(hp == NULL){//for fresh run head is initialized with NULL
            curr = hp = np;
        } else {//If *head isn't NULL, you need to move to the last node first.
            curr = curr->next = np;
        }
    }
    fclose(fp);
    *head = hp;
}
//....................
int main(void){
    //...
    struct node *head = NULL;
    //...
    ReadData(&head);
    //...

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

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