我想从递归函数中写入二进制文件中的一些数据,但我不能。函数应创建树并在特定地址的文件中写入其节点。 功能论证: 深度:树应该是这个depht node_num:tree应该有这么多节点 leaf_num:树应该有这么多叶子 addr_in_f:我需要写入节点的文件中的相对地址 addr_child_node:子节点的reltive地址 depth_now:当前深度为3 num_leaf_now:叶子的当前数量 node_num_now:当前节点数 cv []:包含节点的数组 probem是:函数fwrite将节点写入文件,但不进入正确的地址。我无法弄清楚问题出在哪里。
在 fwrite 之前,我使用快退(...)和 fseek(...)继续 addr_in_f ,然后打印 ftell(...)和 addr_in_f ,它们是相同的值。那么为什么节点没有写在该位置的文件中?
感谢您的帮助!
int create_node(int depth, int node_num, int leaf_num, int addr_in_f, int * addr_child_node, int * depth_now, int * num_leaf_now, int * node_num_now , NodeZI cv[]){
NodeZI * czi;
(*depth_now)++;
if(*node_num_now < node_num){
if(*depth_now == depth ){
(*num_leaf_now)++;
if((*num_leaf_now) <= leaf_num){
czi.left.address= cv[(*num_leaf_now)-1].left.address;
czi.left.key = cv[(*num_leaf_now)-1].left.key ;
czi.right.address= cv[(*num_leaf_now)-1].right.address;
czi.right.key = cv[(*num_leaf_now)-1].right.key ;
FILE * file = fopen(naz_akt_dat, "ab");
if(file == NULL){
printf("EROR!!!");
}
rewind(file);
if(fseek(file,addr_in_f, SEEK_CUR)){
printf("ERR!!!");
}
printf("\ntell: %d, rel_addr: %d", ftell(file),addr_in_f);
fwrite(czi, sizeof(NodeZI), 1, file);
if(fwrite(czi, sizeof(NodeZI), 1, file)!=1){
printf("CAN'T WRITE!!!" );
}
fclose(file);
(*node_num_now)++;
(*depth_now)--;
return czi.left.key ;
}else{
(*depth_now)--;
return -1;
}
}else if((*depth_now) < depth ){
*addr_child_node= *addr_child_node+ sizeof(NodeZI);
czi.left.address= *addr_child_node;
int x = create_node(depth , node_num, leaf_num, *addr_child_node, addr_child_node, depth_now, num_leaf_now, node_num_now, cv);
if(x == -1){
czi.left.address= -1;
}
czi.left.key = x;
*addr_child_node = *addr_child_node + sizeof(NodeZI);
czi.right.address= *addr_child_node;
x = create_node(depth , node_num, leaf_num, *addr_child_node, addr_child_node, depth_now, num_leaf_now, node_num_now, cv);
czi.right.key = x;
if(x == -1){
czi.right.address= -1;
}
FILE * file = fopen(naz_akt_dat, "ab");
rewind(file);
fseek(file, addr_in_f, SEEK_CUR);
if(fwrite(czi, sizeof(NodeZI), 1, file)==1){
printf("CAN'T WRITE!!!");
}
fclose(file);
(*node_num_now)++;
(*depth_now)--;
return czi.left.key ;
}else{
return -1;
}
}else{
czi.left.key = -1;
czi.left.address= -1;
czi.right.key = -1;
czi.right.address= -1;
(*depth_now)--;
return -1;
}
}