0

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.

1 Answer 1

0

If I read and understand your question correctly, it is saying that the program is getting a null pointer for the dictionary and printing out "Could not open a dictionary." If this is the case, then it probably means that the dictionary (default or specified) is not in the correct location relative to your program.

Unless you've changed the code in speller.c, the default dictionary should be in the directory dictionaries and should be named large. For example, if your executable program is in ~/workspace/pset5, then your dictionary should be the file ~/workspace/pset5/dictionaries/large.

Also, if you execute speller with two filenames as parameters, the first will be the dictionary to use and the second will be the file to check. You can test that the program is working by giving an explicit dictionary filename of a file in the current directory.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .