so I am having problems implementing speller, particulary load function. I could find that immediately after I open a dictionary is sets file poiter to NULL, and I don`t know why. Really hope for your help, here are my load function
bool load(const char* dictionary) { FILE* dict = fopen(dictionary, "r"); if (dict == NULL) { printf("Could not open a dictionary"); return 1; } root = calloc(1, sizeof(node)); newptr = calloc(1, sizeof(node)); int letter_index; letter_index = 0; for (int c = fgetc(dict); c != EOF; c = fgetc(dict)) { int l = tolower(c); letter_index = l % 97; if (l == '\n') { newptr->is_word = true; newptr = root; } else if (isalpha(l)) { if (newptr->next[letter_index] == NULL) { newptr->next[letter_index] = (struct node*) calloc(1, sizeof(node)); newptr = newptr->next[letter_index]; } else if (newptr->next[letter_index] != NULL) { newptr = newptr->next[letter_index]; } } else if (l == 39) { letter_index = 26; if (newptr->next[letter_index] == NULL) { newptr->next[letter_index] = (struct node*) calloc(1, sizeof(node)); newptr = newptr->next[letter_index]; } else if (newptr->next[letter_index] != NULL) { newptr = newptr->next[letter_index]; } } } fclose(dict); return false; }
here`s check function as well
bool check(const char* word) { node* trav = root; int index = 0;
for (int i = 0, l = strlen(word); i < l; i++) { if (isalpha(word[i])) { index = tolower(word[index]) % 97; } else if (word[index] == 39) { index = 26; } if (trav->next[index] != NULL) { trav = trav->next[index]; } else return false; } if (trav->is_word) return true; trav = root; return false; }
please feel free to point out other problems, thank you.