gpt4 book ai didi

无法指向从 C 中的文件读取的结构

转载 作者:行者123 更新时间:2023-11-30 14:37:28 25 4
gpt4 key购买 nike

我正在尝试加载一些已保存到 Memberships.txt 二进制文件中的 member 结构。我可以毫无问题地将内容读取到 tmp 中,因为打印结果为我提供了正确的信息。问题似乎出在 tail.next = &tmp; 行。

这个想法是将 head 保留为指针,以便我可以将其传递给稍后将使用的其他函数。 tail 应使用 member head 指向的 member 进行初始化,并且仅用于修改其上的 .next 字段,所以我将正在读取的成员添加到列表末尾。

有人可以帮助我吗?

FILE* f = fopen("Memberships.txt", "rb");
member* head = malloc(sizeof(member));
member tail = *head;
member tmp;
while(fread(&tmp, sizeof(struct member), 1, f) == 1){
printf("Read member %d from file.\n", tmp.number);
printf("Name: %s\n", tmp.first_name);
printf("Surname: %s\n", tmp.second_name);
tail.next = &tmp;
tail = tmp;
}
tail.next = NULL;
printf("Loaded %d members into list!\n\n",tail.number);
if(head->next == NULL){
printf("%d",666);
}

输出:

Read member 1 from file.
Name: Lucas
Surname: Azeve
Read member 2 from file.
Name: Mark
Surname: Lala
Loaded 2 members into list!

666
<小时/>

我已经实现了 @anonmess 建议的更改,例如:

member* head = malloc(sizeof(member));
member tail = *head;
member tmp;
while(fread(&tmp, sizeof(struct member), 1, f) == 1){
member* read = malloc(sizeof(member));
strcpy(read->first_name,tmp.first_name);
strcpy(read->second_name,tmp.second_name);
strcpy(read->address,tmp.address);
strcpy(read->email,tmp.email);
read->number = tmp.number;
read->annual_fee = tmp.annual_fee;
read->status = tmp.status;
read->handicap = tmp.handicap;
read->bracket = tmp.bracket;
read->freq = tmp.freq;
read->rest = tmp.rest;
read->rate = tmp.rate;
printf("Read member %s from file.\n", read->first_name);
printf("Name: %s\n", read->first_name);
printf("Surname: %s\n", read->second_name);
tail.next = read;
tail = *read;
}

获得相同的结果,问题出在最后两行。 head->next 仍然为 NULL。

最佳答案

这是一个独立的示例,您可以修改它来解决您的问题:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#define NAME_LENGTH 20

typedef struct _member {
struct _member *next;
int number;
char first_name[NAME_LENGTH];
char second_name[NAME_LENGTH];
} member;

member member_list[] = {
{ NULL, 1, "Lucas", "Azeve" },
{ NULL, 2, "Mark", "Lala" }
};

void create_file(void) {
FILE *f = fopen("Memberships.txt", "wb");
fwrite(member_list, sizeof(member_list), 1, f);
fclose(f);
}

int main(void)
{
create_file();
FILE* f = fopen("Memberships.txt", "rb");
if (f == NULL) {
perror("Could not open file");
exit(errno);
}
member *head = malloc(sizeof(member));
member *tail = head;
while (1) {
int result;
member *new = malloc(sizeof(member));
result = fread(new, sizeof(*new), 1, f);
if (1 != result) {
if (feof(f)) {
break;
}
errno = ferror(f);
perror("Could not read from file");
exit(errno);
}
printf("Read member %d from file.\n", new->number);
printf("Name: %s\n", new->first_name);
printf("Surname: %s\n", new->second_name);
tail->next = new;
tail = new;
}
fclose(f);
tail->next = NULL;
printf("Loaded %d members into list!\n\n", tail->number);
if (head->next == NULL) {
printf("%d",666);
}
return 0;
}

输出

Read member 1 from file.
Name: Lucas
Surname: Azeve
Read member 2 from file.
Name: Mark
Surname: Lala
Loaded 2 members into list!

希望这将向您展示一种为每个成员分配内存并从文件中读取数据的方法,而无需进行任何不必要的复制。您可以安全地忽略我用于创建该文件的代码。我需要一些数据,并且总是尝试让计算机完成尽可能多的工作。

关于无法指向从 C 中的文件读取的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57310548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com