0

I cannot figure out what I am missing.

Program keeps returning "Could not load dictionaries/large" I have checked and tried different suggestions from similar questions but I do have the folder dictionaries in the same location as my program, I have not changed speller.c, code returns true.. Any ideas? I may have missed the right post for me.

Any suggestion would be appreciated.

This is my load function:

// Counter for words in table
int counter = 0;

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // Open dictionary file
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        return 1;
    }
    // Read stings from the file, one at a time
    char word[LENGTH + 1];
    int index;

    while (fscanf(file, "%s", word) != EOF)
    {
        index = hash(word);
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            free(n);
            return 1;
        }
        strcpy(n->word, word);
        if (table[index] == NULL)
        {
            table[index] = n;
            n->next = NULL;
        }
        else
        {
            n->next = table[index];
            table[index] = n;
        }
        counter++;
        free(n);
    }
    fclose(file);
    return 0;
}

Thank you in advance.


edit 1: Here's a pic of the IDE screenshot

// Implements a dictionary's functionality

#include "dictionary.h"

// Represents a node in a hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// Number of buckets in hash table
const int N = 65536;

// Hash table
node *table[N];

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    // Hash to obtain a hash value
    int index = hash(word);
    node *cursor = malloc(sizeof(node));
    // Access linked list at that index in the hash table
    while (cursor != NULL)
    {
        cursor = table[index];
        // Traverse linked list, looking for the word
        if (strcasecmp(cursor->word, word) == 0)
        {
            return 0;
        }
        else
        {
            cursor = cursor->next;
        }
    }
    return 1;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    unsigned int hash = 0;
    for (int i = 0, n = strlen(word); i < n; i++)
    {
        hash = (hash << 2) ^ word[i];
    }
    return hash % N;
}
// Counter for words in table
int counter = 0;

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // Open dictionary file
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        return 1;
    }
    // Read stings from the file, one at a time
    char word[LENGTH + 1];
    int index;

    while (fscanf(file, "%s", word) != EOF)
    {
        index = hash(word);
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            free(n);
            return 1;
        }
        strcpy(n->word, word);
        if (table[index] == NULL)
        {
            table[index] = n;
            n->next = NULL;
        }
        else
        {
            n->next = table[index];
            table[index] = n;
        }
        counter++;
        free(n);
    }
    fclose(file);
    return 0;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    // TODO
    if (counter > 0)
    {
        return counter;
    }
    return 0;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    // TODO
    for (int i = 0; i < N; i++)
    {
        node *cursor = table[i];
        do
        {
            node *tmp = cursor;
            cursor = tmp->next;
            free(tmp);
        }
        while (cursor != NULL);
        free(cursor);
        free(table);
        return true;
    }
    return false;
}
1

[EDIT - previous answer deleted, irrelevant]

Haven't seen this in a while. The problem lies with your return statements in the load function. You are returning 0's and 1's, not true or false. This can be confusing, especially when you confuse it with another programming standard of returning 0 for success and a non-zero for errors.

The load() function requires returning a bool, not a number, so as best practice, you should always return true or false when a bool is called for. (Always return the correct type from a function, not something that has to be cast or interpreted.) The code is returning 0 for success and 1 for failure. Unfortunately, 0 is always interpreted as false and 1 (or any other non-zero, + or -) is interpreted as true. This code is (apparently) completing the load function, but returning false, so the code in speller.c sees it as a failure and prints the "could not load" message.

99 times out of 100, this is a failure to have the dictionary file in the correct place. Your error is the 1 in 100! ;-)

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

| improve this answer | |
  • Try executing your code again and grab a screenshot of your ENTIRE IDE and post it in the question. – Cliff B Jun 2 at 19:44
  • Thank you so much. It now it returns segmentation fault and also valgrind gives invalid read of size 1 plus memory leaking. I don't really get why valgrind was returning 0 leaks before changing the false/true... I will figure it out or open a new post with the appropriate issue if I am unable to fix it. – meh362 Jun 3 at 14:57

You must log in to answer this question.

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