为什么readfile函数直接出错、导致程序错误跳出且返回值错误?

新手上路,请多包涵

一个基本的使用二叉树读入文件中单词(仅为测试知识综合的第一步学习,暂不考虑一个单词分居两行、首字母大写转换为同单词等等情况)。顺了几遍感觉并没有太大的问题,可是实验直接出错,且调试时gdb显示No symbol table info available.没有什么解决思路,所以在此提问。麻烦了,谢谢!

#include <stdio.h>
#include <stdlib.h>
typedef struct words
{
    char word[15];
    int n;
} item;
item * node;
typedef struct nodes
{
    item gather;
    struct nodes * left;
    struct nodes * right;
} Node;
void readfile(FILE * fp, Node *);
int main()
{
    FILE *fp;
    fp = fopen("try.txt", "r");
    Node *root;
    root = (Node *)malloc(sizeof(Node));
    ((root->gather).word[0]) = '\0';
    ((root->gather)).n = 0;
    root->left = root->right = NULL;
    readfile(fp, root);
    printf("%s", root->gather.word);
    return 0;
}
void readfile(FILE * fp, Node * root)
{
    char ch;
    char cht;
    Node *test;
    while (!feof(fp))
    {
        test = root;
        ch = getc(fp);
        FILE *helper = fp;
        int n = 1;
        if ((test->gather).word[0] == ch)
            while ((cht = getc(helper)) != '\n' && (cht ) != ' ')
            {
                if ((test->gather).word[n] == cht)
                {
                    n++;
                    continue;
                }
                else
                    break;
            }
        if ((cht ) == '\n' && (cht) == ' ' && (test->gather).word[n] == cht)
        {
            test->gather.n++;
            fp = helper;
        }
        while (test->gather.word[0] != '\0')
        {
            if ((ch < (test->gather).word[0]))

                test = test->left;

            else
                test = test->right;
            test = (Node *)malloc(sizeof(Node));
            ((test->gather).word[0]) = '\0';
            ((test->gather)).n = 0;
            test->left = test->right = NULL;

        }
        if (test->gather.word[0] == '\0')
        {
            test->gather.word[0] = ch;
            test->gather.n++;
            int x = 1;
            while ((ch = getc(fp)) != '\0' && (ch) != ' ')
            {
                test->gather.word[x] = ch;
                x++;
            }
            test->gather.word[x] = '\0';
        }
    }
}
阅读 1.4k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题