I think there might be something wrong with the fscanf use because when using gbd temp's value never changes, it stays as NULL. Also, word's value stays as 0 throughout the entire program and every word in the text file appears as misspelled. Can someone help me find the error?
bool load(const char* dictionary)
{
root = newnode();
//open dictionary file
FILE* dict = fopen(dictionary, "r");
if (dict == NULL)
{
printf("Could not open dictionary\n");
return false;
}
//retrieve words from dictionary
char* temp = NULL;
while (fscanf(dict, "%s", temp) == 1)
{
insert(root, temp);
words++;
}
return true;
}
NOTE: root and words variables are global variables.
Relevant code:
#define ALEN 27
//global variables
node* root;
int words;
//trie nodes' definition.
typedef struct trie
{
bool is_word;
struct trie* children[ALEN];
} node;
//create a node
node* newnode(void)
{
node* new_node = malloc(sizeof(node));
new_node->is_word = false;
for (int i = 0; i < ALEN; i++)
{
new_node->children[i] = NULL;
}
return new_node;
}
//insert word into trie
void insert(node* root, char* temp)
{
int len = strlen(temp);
node* trav = root;
for (int i = 0; i < len; i++)
{
if (trav->children[temp[i] - 'a'] == NULL)
{
trav->children[temp[i] - 'a'] = newnode();
}
trav = trav->children[temp[i] - 'a'];
}
trav->is_word = true;
}