标题:交叉链表二进制保存问题
只看楼主
nnak7h6j
Rank: 1
等 级:新手上路
帖 子:3
专家分:7
注 册:2007-7-14
 问题点数:0 回复次数:1 
交叉链表二进制保存问题
这阵子在做链表的保存和读取,二进制fwrite和fread,两个函数代码如下
程序代码:
void savedata(urban *head){
     FILE *out1,*out2,*out3;
     char ch[100];
     urban *p1= head;
     tree *p2= NULL;
     tree_i *p3= NULL;
     //printf("choose a directory.\n");
     //scanf("%s",ch);
     if((out1=fopen("c:\\resource1.dat","wb"))== NULL)
         exit(-1);
     if((out2=fopen("c:\\resource2.dat","wb"))== NULL)
         exit(-1);
     if((out3=fopen("c:\\resource3.dat","wb"))== NULL)
         exit(-1);
     while(p1!=NULL){
                      fwrite(p1,sizeof(urban),1,out1);
                      p2=p1->head;
                      while(p2!=NULL){
                           fwrite(p2,sizeof(tree),1,out2);
                           p3 = p2->head;
                           while(p3!=NULL){
                                    fwrite(p3,sizeof(tree_i),1,out3);
                                    p3 = p3->next;
                                          }
                           p2 = p2->next;
                                      }
                      p1 = p1->next;
                      }
      fclose(out1);
      fclose(out2);
      fclose(out3);
      }     

void loaddata(urban **head){
     FILE *in1,*in2,*in3;
     urban *hp1 = NULL, *p1;
     tree *hp2 = NULL, *p2;
     tree_i *hp3=NULL, *p3;
     if((in1 = fopen("c:\\resource1.dat","rb"))==NULL)
         exit(-1);
     if((in2 = fopen("c:\\resource2.dat","rb"))==NULL)
         exit(-1);
     while(!feof(in1)){
            p1=(urban *)malloc(sizeof(urban));
            fread(p1,sizeof(urban),1,in1);
            if(!feof(in1)){
                  p1->head =NULL;
                  p1->next =hp1;
                  hp1 = p1;
                       }
                       }
     (*head)=p1;
     while(!feof(in2)){
            p2 = (tree *)malloc(sizeof(tree));
            fread(p2,sizeof(tree),1,in2);
            if(!feof(in2)){
                  p1 = (*head);
                  while(p1!=NULL){
                            if(!strcmp(p1->ar_num,p2->ar_num)){
                                     p2->next = p1->head;
                                     p1->head = p2;
                                     if((in3 = fopen("c:\\resource3.dat","rb"))==NULL)
                                          exit(-1);
                                     while(!feof(in3)){
                                               p3=(tree_i *)malloc(sizeof(tree_i));
                                               fread(p3,sizeof(tree_i),1,in3);
                                               if(!feof(in3)){
                                                     if(!strcmp(p2->tree_num,p3->tree_num)){
                                                           p3->next =hp3;
                                                           hp3 = p3;
                                                                            }
                                                     else free(p3);
                                                     }
                                                     }
                                      fclose(in3);
                                      p2->head = p3;
                                      break;
                                      }
                            else p1=p1->next;
                            }
                            }
                            }
     fclose(in1);
     fclose(in2); 
     }

附上我3个自定义结构,
程序代码:
typedef struct urban_basic
{
    char ar_num[7];
    char ar_name[20];
    char dep[30];
    char add[30];
    char tel[13];
    char manager[20];
    struct urban_basic *next;
    struct tree_info *head;
}urban;

typedef struct tree_info
{
    char ar_num[7];
    char tree_num[5];
    char name[20];
    char sci_name[20];
    char ctl[20];
    int age;
    char con_time[11];
    char level;
    char add[30];
    struct tree_info *next;
    struct tree_basic *head;
}tree;

typedef struct tree_basic
{
    char ar_num[7];
    char tree_num[5];
    char ppl[20];
    char date[11];
    char method[30];
    char health[20];
    struct tree_basic *next;
}tree_i;
2个函数里面,可能我比较菜,看不出什么逻辑的错误。
运行的时候,到链表读取就崩溃了,debug时,发现对主链读取存在错误,估计save那里也有问题。
请各位大大不吝赐教。



[ 本帖最后由 nnak7h6j 于 2010-10-9 00:40 编辑 ]
搜索更多相关主题的帖子: 链表 二进制 交叉 保存 
2010-10-08 23:57
nnak7h6j
Rank: 1
等 级:新手上路
帖 子:3
专家分:7
注 册:2007-7-14
得分:0 
没人进来指点下么?
2010-10-09 02:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-322294-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.592099 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved