0

I would be so grateful to anyone who can help me figure out this issue.

After finally getting my code to compile I was met with the following terminal error when attempting to run the program:

"Could not load dictionaries/large."

enter image description here

Here is my dictionary.c so far:

// Implements a dictionary's functionality

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "dictionary.h"

typedef struct node
{
    bool is_word;
    struct node* children[27];
}node;

node* first;

int word_count = 0;
int get_index(char c);
node* create_node();

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    node* node_point = first;
    for (int i = 0, len = strlen(word); i <= len; i++)
    {
        if (word[i] == '\0')
        {
            if (node_point->is_word == true)
                return true;
            else
                return false;
        }
        int index = get_index(word[i]);
        if (node_point->children[index] == NULL)
            return false;

        node_point = node_point->children[index];
    }
    return false;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    FILE* dict_point = fopen(dictionary, "r");
    if (dict_point == NULL)
    {
        fprintf(stderr, "File not found\n");
        return false;
    }

    //global first gets new node
    first = create_node();

    //c gets char from dictionary and check till EOF

    node* node_point = first;
    for (char c = fgetc(dict_point); c != EOF; c = fgetc(dict_point))
    {
        //if the letter is not a newline char then we need to load this in trie
        if (c != '\n')
        {
            int index = get_index(c);
            if (node_point->children[index] == NULL)
            {
                node_point->children[index] = create_node();
            }
            node_point = node_point->children[index];
        }
        else
        {
            //indicate end of the word
            node_point->is_word = true;
            //increment word count - found word from dict_point
            word_count++;
            //back to beginning
            node_point = first;
        }
    }
    return false;
}

//returns the index of the character entered
int get_index(char c)
{
    if (c == '\'')
        return 26;
    else if (c >= 'a' && c <= 'z')
        return c - 'a';
    else if (c >= 'A' && c <= 'Z')
        return c - 'A';

    return -1;

}

//returns pointer to node with initial values: false and NULL
node* create_node()
{
    node* node_point = malloc(sizeof(node));
    node_point->is_word = false;
    for (int i = 0; i < 27; i++)
    {
        node_point->children[i] = NULL;
    }
    return node_point;
}

// Returns # of words in dictionary if able to load, else 0 if not
unsigned int size(void)
{
    return word_count;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    // TODO
    return false;
}
0

Ugh! I figured it out. I was returning "false" from the load function. Can't believe I missed that!

Resolved.

| improve this answer | |

You must log in to answer this question.

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