从二进制文件读链表的问题

来源:百度知道 编辑:UC知道 时间:2024/05/19 11:30:49
我写入时是这样写的:
fp=fopen(fname,"wb+");
fwrite(&compno,sizeof(int),1,fp);
for(p=head;p!=NULL;p=p->next){
fwrite(p,sizeof(struct company),1,fp);
for(p1=p->head;p1!=NULL;p1=p1->next){
fwrite(p1,sizeof(struct person),1,fp);
for(p2=p1->head;p2!=NULL;p2=p2->next)
fwrite(p2,sizeof(struct info),1,fp);
请问要重新读到内存中应该怎样读呢??
这个表的结构是这样的(data部分没写出来)
struct info{
struct info *next;
struct info *last;
};
struct person{
struct person *next;
struct person *last;
struct info *head;
};
struct company{
struct company *next;
struct person *head;
struct company *last;
};
另外,我读出来后,要不要重新把各个结点重新连接一次?
我把person等链链到company里面,那么struct company的大小是怎样算的?

结构大小直接用WATCH sizeof就可以看到了,然后自己再算一下吧.
动态内存分配,应该是要重新链的.
p=(struct company *)malloc(sizeof(...));
fread(p,sizeof(...),1,fp);
head=p;
p1=(struct person *)malloc(sizeof(...));--------------(2)
fread(p1,...);
p->head=p1;-------------------------(1)
p2=(struct info *)malloc(...)
fread(p2,...);
while(p2->next!=NULL)
{
tail2=p2;
p2=(struct info *)malloc(...)
fread(p2,...);
tail2->next=p2;
p2->last=tail2;
}----------------------------------(1)
while(p1->next!=NULL){
tail1=p1;
p1=...;
fread(...);
tail1->next=p1;
p1->last=tail1;
(1)
}---------------------------------------------------(2)
while(p->next!=NULL){
tail=p;
p=...;
fread();
tail->next=p;
p->last=tail;
(2)