0

I dont know what the problem with my code. It doesn't load any dictionaries. I tried to test whether it can open a dictionary provided as a parameter but returns could not load small. Any recomendations?

bool load(const char* dictionary)

{

    // TODO
    // opens dictionary
    FILE* file = fopen(dictionary, "r");
    if (file == NULL){
        return false;
    }

    // create an array for word to be stored in
    char word[LENGTH+1];

    // scan through the file, loading each word into the hash table
    while (fscanf(file, "%s\n", word)!= EOF)
    {
        // increment dictionary size
        dictionarySize++;

        // allocate memory for new word 
        node* newWord = malloc(sizeof(node));

        // put word in the new node
        strcpy(newWord->word, word);

        // find what index of the array the word should go in
        int index = hash(word);

        // if hashtable is empty at index, insert
        if (hashtable[index] == NULL)
        {
            hashtable[index] = newWord;
            newWord->next = NULL;
        }

        // if hashtable is not empty at index, append
        else
        {
            newWord->next = hashtable[index];
            hashtable[index] = newWord;
        }      
    }

    // close file
    fclose(file);

    // return true if successful 
    return true;
}
2
  • 1
    In this case it will be necessary to know what errors you have in the console
    – MARS
    May 26 '17 at 22:42
  • Have you tried using printf() to look at what your code is doing? BTW, malloc() might return NULL (i.e. if you run out of memory).
    – Jo Are By
    May 27 '17 at 13:14
1

Is it possible that the dictionary small is not in the current directory? Or is it possible that its location doesn't match the parameter that you're passing to the program? Try running ls to check that the dictionary is in the current directory.

If you pass only the file name of the dictionary as a parameter, it must be in the current directory. If you pass a relative path or absolute path with the filename, the dictionary file must be in that location. Otherwise, the program can't find it and will report this error.

If this isn't the issue, then you need to run ls and run the program, get a screen capture, and add it to the question.

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 .