尝试将树的内容保存到文件时,没有保存任何内容,我不知道为什么。这些是我正在进行的函数调用:
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;
}
任何帮助将不胜感激。