Can';t指向C中从文件读取的结构

Can';t指向C中从文件读取的结构,c,C,我正在尝试加载一些已保存到我的Memberships.txt二进制文件中的成员结构。我可以毫无问题地将内容读入tmp,因为印刷品提供了正确的信息。问题似乎出在tail.next=&tmp行 这样做的目的是将head作为指针,以便我可以将其传递给稍后使用的其他函数tail应该初始化为成员head指向并将仅用于修改。下一个字段,因此我将正在读取的成员添加到列表末尾 有人能帮我吗 FILE* f = fopen("Memberships.txt", "rb"); member* head = mall

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

这样做的目的是将
head
作为指针,以便我可以将其传递给稍后使用的其他函数
tail
应该初始化为
成员
head
指向并将仅用于修改
。下一个
字段,因此我将正在读取的成员添加到列表末尾

有人能帮我吗

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

我已经实施了@Anonmes建议的更改,如:

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
仍然为空。

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

#包括
#包括
#包括
#定义名称\u长度20
typedef结构成员{
结构_成员*下一步;
整数;
字符名[名称长度];
字符第二个名称[名称长度];
}成员;
成员名单[]={
{NULL,1,“Lucas”,“Azeve”},
{NULL,2,“标记”,“拉拉”}
};
void创建_文件(void){
文件*f=fopen(“Memberships.txt”、“wb”);
fwrite(成员列表),sizeof(成员列表),1,f;
fclose(f);
}
内部主(空)
{
创建_文件();
文件*f=fopen(“Memberships.txt”、“rb”);
如果(f==NULL){
perror(“无法打开文件”);
出口(errno);
}
成员*head=malloc(sizeof(成员));
成员*尾部=头部;
而(1){
int结果;
成员*new=malloc(sizeof(成员));
结果=fread(新尺寸(*新尺寸),1,f);
如果(1!=结果){
if(feof(f)){
打破
}
errno=ferror(f);
perror(“无法从文件中读取”);
出口(errno);
}
printf(“从文件中读取成员%d。\n”,新建->编号);
printf(“名称:%s\n”,新建->名字);
printf(“姓氏:%s\n”,新->第二名);
尾部->下一步=新建;
尾巴=新的;
}
fclose(f);
tail->next=NULL;
printf(“已将%d个成员加载到列表!\n\n”,尾部->编号);
if(head->next==NULL){
printf(“%d”,666);
}
返回0;
}
输出

从文件中读取成员1。
姓名:卢卡斯
姓:艾泽夫
从文件中读取成员2。
姓名:马克
姓拉拉
已将2个成员加载到列表中!

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

对于链接列表,您需要动态分配链接。因此
tmp
应该是一个指针,并且在每个循环的开始处
malloc
它。现在,你只是一次又一次地覆盖同一个内存。我怎么知道存储了多少个对象?如果没有使用
fread(&tmp,sizeof(struct member),1,f)==1
@anonmess,如果我在while的正下方malloc,如何将来自tmp的信息正确写入分配的指针?!我想你可以保持
tmp
的原样,在复制
tmp
@LucasAzevedo的数据后,在
while()
条件下,如果我将
fread()
调用放在
while()
条件下,你可以使用另一个指针
malloc
并将每个循环添加到列表中。然后您可以直接读取新的
malloc()
'd内存。非常感谢!我有问题的选择,而不是有fread内的时间!一尘不染的代码,只是忘记了errno之前的int?不,
errno
提供。如果您的编译器在抱怨,那么您忘记包含它了。