1

Every time I try to run speller.c I get an output of:

  • Could not open dictionary
  • Could not load dictionaries/large

I have a feeling that it is to do with the location of the dictionaries but I have moved them into the same directory, re downloaded the dictionaries, checked permissions and changed the definition of where to find the dictionaries. I now believe there is a problem with my code

This is my dictionary.c

/**
 * Implements a dictionary's functionality.
 */


#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "dictionary.h"

int wordcount = 0;
char *word = NULL;

typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

node *htable[HASHTABLE_SIZE];

int hashit(char *hashing)
{
    unsigned int hash = 0;
    for (int i = 0, j = strlen(hashing); i < j; i++)
{
    hash = (hash << 2) ^ hashing[i];
}
return hash % HASHTABLE_SIZE;

}

/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char *word)
{
    // TODO
    return false;
}

/**
 * Loads dictionary into memory. Returns true if successful else     false.
 */
bool load(const char *dictionary)
{
    for (int i = 0; i < HASHTABLE_SIZE; i++)
{
    htable[i] = NULL;
}
FILE *fp = fopen("dictionary", "r");
if (fp == NULL)
{
    fprintf(stderr, "Could not open dictionary\n");
    return false;
}
while (fscanf(fp, "%s", word) != EOF)
{
    node *newnode = malloc(sizeof(node));
    if (newnode == NULL)
    {
        fprintf(stderr, "Could not malloc new node\n");
        return 3;
    }
    wordcount++;

    int hashed = hashit(newnode->word);
    node *top = htable[hashed];

    if (top == NULL)
    {
        htable[hashed] = newnode;
    }

    else
    {
        newnode->next = htable[hashed];
        htable[hashed] = newnode;
    }
}


return false;
}

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

/**
 * Unloads dictionary from memory. Returns true if successful else false.
 */
bool unload(void)
{
    // TODO
    return false;
}
2

At a quick scan, this FILE *fp = fopen("dictionary", "r"); looks like a problem. By enclosing dictionary in quotes, it becomes a string literal. The program will look for a filed named dictionary in the present working directory. If the quotes are removed, the program will look for the contents of the variable dictionary as received by the function.

2
  • Also, "could not load" is because you are returning false from the load function. – curiouskiwi Jan 19 '18 at 5:04
  • Thank you I cant believe I didn't see that – Benjamin Norton Jan 23 '18 at 11:00

You must log in to answer this question.

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