load function looks good I think the error in speller.c when I wrote ./speller texts/lalaland.txt output: MISSPELLED WORDS Could not unload dictionaries/large. when wrote ./speller text output: Could not open text. I also try to remove fclose in load function and no difference.
bool load(const char *dictionary)
{
node *n;
char word[LENGTH + 1];
int number;
FILE *f = fopen(dictionary, "r");
if (f == NULL)
{
return 1;
}
while (fscanf(f, "%s", word) != EOF)
{
dicSize++;
n = malloc(sizeof(node));
if (n == NULL)
{
return 1;
}
strcpy(n-> word, word);
n->next = NULL;
number = hash(word);
if (table[number] == NULL)
{
table[number] = n;
}
else
{
n->next = table[number];
table[number] = n;
}
}
// for (int i = 0; i < 1; i++)
// {
// for (node *j = table[i]; j != NULL; j = j->next)
// {
// printf("the word: %s\n", j->word);
// }
// }
dicSize = true;
fclose(f);
return true;
}
I think the error here
int main(int argc, char *argv[])
{
// Check for correct number of args
if (argc != 2 && argc != 3)
{
printf("Usage: ./speller [DICTIONARY] text\n");
return 1;
}
// Structures for timing data
struct rusage before, after;
// Benchmarks
double time_load = 0.0, time_check = 0.0, time_size = 0.0, time_unload = 0.0;
// Determine dictionary to use
char *dictionary = (argc == 3) ? argv[1] : DICTIONARY;
// Load dictionary
getrusage(RUSAGE_SELF, &before);
bool loaded = load(dictionary);
getrusage(RUSAGE_SELF, &after);
// Exit if dictionary not loaded
if (!loaded)
{
printf("Could not load %s.\n", dictionary);
return 1;
}
// Calculate time to load dictionary
time_load = calculate(&before, &after);
// Try to open text
char *text = (argc == 3) ? argv[2] : argv[1];
FILE *file = fopen(text, "r");
if (file == NULL)
{
printf("Could not open %s.\n", text);
unload();
return 1;
}