C 使用fwrite/fread保存和加载树

C 使用fwrite/fread保存和加载树,c,save,fwrite,fread,red-black-tree,C,Save,Fwrite,Fread,Red Black Tree,我是C语言中的noob,我必须保存并加载一个二叉树(红黑树),它的结构作为节点,每个节点都是RBdata: typedef struct RBdata_ { // The variable used to index the tree has to be called "key". TYPEKEY key; //key is of type char* int numFitxers; int *fileCount; } RBdata; 每个节点如下所示: typedef s

我是C语言中的noob,我必须保存并加载一个二叉树(红黑树),它的结构作为节点,每个节点都是RBdata:

typedef struct RBdata_
{
  // The variable used to index the tree has to be called "key".
  TYPEKEY key; //key is of type char*  
  int numFitxers;
  int *fileCount;
} RBdata;
每个节点如下所示:

typedef struct Node_ {
    /* For internal use of the structure. Do not change. */
    struct Node_ *left;         /* left child */
    struct Node_ *right;        /* right child */
    struct Node_ *parent;       /* parent */
    nodeColor color;            /* node color (BLACK, RED) */

    /* Data to be stored at each node */
    RBdata *data;                    /* data stored in node */
} Node;
我的问题是,我不知道我的错误在哪里,在save函数中还是在load函数中:

RBTree loadTree(char *name){
    RBTree tree;
    initTree(&tree);
    RBdata *data;
    FILE *fp;
    if((fp=fopen(name,"r"))!=NULL){
        data=malloc(sizeof(RBdata));
        while(fread(&data->key,sizeof(char),MAXCHAR,fp)==1){
            fread(&data->numFitxers,sizeof(int),1,fp);
            data->fileCount=malloc(sizeof(int)*595);
            fread(&data->fileCount,sizeof(int),595,fp);
            insertNode(&tree,data); 
            data=malloc(sizeof(RBdata));
        }
        fclose(fp);
    }
    else{
        printf("Error: Cannot read the file.\n");
    }
    return tree;
}


void saveTree(Node *a, FILE * file1)
{
    if (a->right != NIL)
        saveTree(a->right,file1);
    if (a->left != NIL)
        saveTree(a->left,file1);
    fwrite(a->data->key, sizeof(char),MAXCHAR,file1); //key, numFiles i fileCount
    fwrite(&a->data->numFitxers, sizeof(int),1,file1);
    fwrite(a->data->fileCount, sizeof(int),595,file1);      

}
当试图加载一棵树时,我得到一个“分段错误”。
你能给我一些帮助吗?谢谢

,您的问题是?我试图加载树,但出现了分段错误。我不知道我做错了什么,很抱歉不知道错误在哪里。您正在传递指向fread指针的指针,特别是data->key和data->fileCount(删除&)。数据->键也不指向任何分配的内存。