0

我是 C 中的菜鸟,我必须保存并加载一个具有结构作为节点的二叉树(红黑树),每个节点都是 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;

我的问题是我不知道我的错误在哪里,在保存功能或加载功能中:

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);      

}

尝试加载树时,出现“分段错误”。你能给我一些帮助吗?谢谢

4

0 回答 0