【发布时间】:2016-05-19 12:52:11
【问题描述】:
在过去的 2 天里,我遇到了一个奇怪的问题,我还不能解决它。我正在尝试从 2 个文本文件中获取单词并将这些单词添加到树中。我选择的获取单词的方法在这里引用: Splitting a text file into words in C.
我用来向树中插入单词的函数如下:
void InsertWord(typosWords Words, char * w)
{
int error ;
DataType x ;
x.word = w ;
printf(" Trying to insert word : %s \n",x.word );
Tree_Insert(&(Words->WordsRoot),x, &error) ;
if (error)
{
printf("Error Occured \n");
}
}
正如发布的链接中所述,当我尝试将文本文件中的单词导入树时,我得到“发生错误”。再次使用函数:
文本文件:
一个
啊啊
啊啊啊
char this_word[15];
while (fscanf(wordlist, "%14s", this_word) == 1)
{
printf("Latest word that was read: '%s'\n", this_word);
InsertWord(W,this_word);
}
但是当我用以下方式插入完全相同的单词时,它就可以正常工作了。
for (i = 0 ; i <=2 ; i++)
{
if (i==0)
InsertWord(W,"a");
if (i==1)
InsertWord(W,"aaah");
if (i==2)
InsertWord(W,"aaahh");
}
这证明树的功能工作正常,但我无法理解当时发生了什么。我正在调试 2 天,但仍然无法弄清楚。有什么想法吗?
【问题讨论】:
-
我想我们需要看看
Tree_Insert()的源代码。
标签: c file binary-search-tree