C 预订单从文件中填充树

C 预订单从文件中填充树,c,C,我想知道你是否能帮我,我真的被我写的代码卡住了。我应该按照以下方式从一个文件中预先填充一棵树 Hello goodbye yes ' ' <- This is an actual space in the file no end of file 这个函数应该按预定顺序填充我的树。我在这里使用readFile函数获取数据 char *readFile(FILE *fp) { int c = 0; int i = 0;

我想知道你是否能帮我,我真的被我写的代码卡住了。我应该按照以下方式从一个文件中预先填充一棵树

    Hello
    goodbye
    yes
    '    ' <- This is an actual space in the file
    no
    end of file
这个函数应该按预定顺序填充我的树。我在这里使用readFile函数获取数据

    char *readFile(FILE *fp)
{
   int c = 0;
   int i = 0;
   size_t capacity = 10; 
   char *arr = NULL;
   char* temp = NULL;
   arr = (char*) malloc(capacity * sizeof(char));
   while (((c = fgetc(fp)) != '\n') && (c != EOF))
   { 
  if(capacity == (i+2))
       {
   capacity *= 2 ;
   temp = (char*)realloc(arr,capacity * sizeof(char));
    /*Assigns Address to newly allocated array*/ 
    arr = temp;
      }
    arr[i] = c;
    i++;
    }
   arr[i-1] = '\0';
    if(arr[0] == '\r') /* Line spaces to move up*/
     {
    printf("Got here SPACE\n");
    arr[0] = ' ';
     }
   else if(c == EOF)/*if its an end of file return*/
     {
   printf("Got here EOF \n");
   arr[0] = ' ';
      } 
 return arr;
 }
我填充树的递归算法称为“填充”。我并不擅长递归,但我已经多次浏览了我的代码,看不出有什么明显的错误。目前看来我只能打印根。尝试读取左或右根数据会立即出现seg故障

    void populate(FILE *fp,struct node *root)
   {
    char *data = readFile(fp);
    if(data[0] == ' ') /*possible issues with usingn null charater?   */ /*statement is true for both EOF and spaces*/
   {
     return;
    }
     printf("Creating Node\n");
     if(root == NULL)
     {
     root = createNode();/* create the current root */
     root->data = data;/*assign data*/
     }
     printf("%s\n",data);
     populate(fp,root->left);
     populate(fp,root->right);
   }
我的主要

    int main(int argc, char *argv[])
    {   
     FILE *fp;
     node *root = createNode();
     fp = fopen("qa.db","r+");
      if(fp == NULL)
     {
     printf("File not open");
     } 
    populate(fp,root);
     if(root == NULL){
        printf("Equal to NULL\n");
     }
     printTree(root);
     /*check = checkFile(fp)*/
    fclose(fp);
    return 0;

populate()无法更改调用者中的root*。如果它以NULL开头,它将永远保持NULL。真的很抱歉,你是什么意思?在我的主函数中,我首先将root设置为一个新节点,然后再将其传入。我已经添加了主函数。'root=createNode();/*创建当前根*/使用指向新节点的指针加载本地参数。当函数返回时,该局部变量将丢失。换句话说,“填充(fp,root->left);”没有,也不能修改“root->left”。好的,我明白你的意思。谢谢你的帮助!有没有解决这个问题的建议?我应该在main中的某个位置创建数组中所需的所有节点吗?不-尝试将签名更改为“node*populate(FILE*fp,struct node*root)”,在末尾添加一个“return node”,并像root->left=populate(fp,root->left);”这样调用。反正是这样的。。。。
    int main(int argc, char *argv[])
    {   
     FILE *fp;
     node *root = createNode();
     fp = fopen("qa.db","r+");
      if(fp == NULL)
     {
     printf("File not open");
     } 
    populate(fp,root);
     if(root == NULL){
        printf("Equal to NULL\n");
     }
     printTree(root);
     /*check = checkFile(fp)*/
    fclose(fp);
    return 0;