私は 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);
}
ツリーを読み込もうとすると、「セグメンテーション違反」が発生します。助けてもらえますか?ありがとう