I hope i don't get voted down to quickly for this, but I have a project I'm working on for school in which I have to build a spell checker. I decided to use a trie, and it seems to be working, but I have a bug I can't find. I think the issue is in the following,
bool load(const char* dictionary)
{
if (!rootNode)
{
rootNode = trieNodeCreate();
if (!rootNode)
{
printf("could not allocate root node");
return false;
}
}
// Open the file
FILE* fp = fopen(dictionary, "r");
if (fp == NULL)
{
printf("could not open dictioanry %s\n", dictionary);
return false;
}
int index = 0;
for (int c = fgetc(fp); c != EOF; c = fgetc(fp))
{
char word[LENGTH];
if (c != '\n' )
{
word[index] = c;
index++;
}
else
{
trieWordInsert(word, rootNode);
index = 0;
wordCount ++;
}
}
fclose(fp);
if (wordCount)
{
return true;
}
return false;
}
but I've been unable to find it. The rest of the project can be found at
I think you didn't end the word with a '\0'
char word[LENGTH];
if (c != '\n' )
{
word[index] = c;
index++;
}
else
{
word[index] = '\0'; //missing this one!!!
trieWordInsert(word, rootNode);
index = 0;
wordCount ++;
}
I think you better use fscanf
and read the file word by word.