ツリーの内容をファイルに保存しようとすると、何も保存されず、理由がわかりません。これらは私が行っている関数呼び出しです:
insert(tree, cust.id, filePos);
saveTree(tree, "cIndex.dat", 0);
そして、これらは関連する機能です:
struct NODE *insert(struct NODE *node, char info[], int pos)
{
int length = strlen(info);
// 1. If the tree is empty, return a new, single node
if (node == NULL)
{
struct INFO stuff;
//stuff = (struct INFO*)malloc(sizeof(struct INFO));
strncpy(stuff.data, info, DATA_LENGTH);
stuff.filePos = pos;
node = malloc(sizeof(struct NODE));
node->data = stuff;
node->left = NULL;
node->right = NULL;
return(node);
}
else
{
// 2. Otherwise, recur down the tree
if (strncmp(info, node->data.data, length) <= 0)
{
node->left = insert(node->left, info, pos);
}
else
{
node->right = insert(node->right, info, pos);
}
return(node); // return the (unchanged) node pointer
}
}
int saveTree(struct NODE *node, char filename[], int posInFile)
{
FILE *file;
file = fopen(filename, "r+b"); //r+ so as to overwrite existing data instead of appending
char c;
char posStr[POS_LENGTH];
fseek(file, posInFile, SEEK_SET);
while((c = fgetc(file)) != EOF)
{
if(node != NULL)
{
fseek(file, -1, SEEK_CUR);
fwrite(node->data.data, sizeof(char), DATA_LENGTH, file);
sprintf(posStr, "%d", node->data.filePos);
fwrite(posStr, sizeof(char), POS_LENGTH, file);
posInFile = ftell(file);
saveTree(node->left, filename, posInFile);
saveTree(node->right, filename, posInFile);
}
}
fclose(file);
return 1;
}
どんな助けでも大歓迎です。