【发布时间】:2017-10-31 04:17:05
【问题描述】:
我编写了一个 C 代码来使用链表实现字典(节点按排序顺序) 我想将数据保存到文件中,并且能够在下次运行程序时重新加载数据。我无法从文件中加载数据。这是我的数据读写代码:
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 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);
}
我使用了sizeof,而不是strlen,它是一个字符串。最后会有空字符 - 字符串没问题。但它会消耗更多的内存。
【问题讨论】:
-
那个不是用来读的。它是用来写数据到下一个节点的。阅读将由
fread完成 -
in
ReadData读取由freadif-else部分用于修复节点之间的链接 -
在
else部分head->next = tail将新节点的地址分配给head指向的节点的next文件。现在我们必须将头指向新节点(尾),新节点的地址分配给前一个节点(head->next = tail)的next字段。因此,为了将head指向新节点,我使用head = head->next作为head->next=tail的地址 -
好的.. 但我认为这是一回事。因为
head->next持有tail的地址 -
主要问题是你不能在函数中改变调用者的
head。在ReadData中,不需要任何参数。head应该被返回。
标签: c file struct io linked-list