0

I have a question concerning my load function. I get the error that the dictionary does not load. Does anyone recognize the mistake? Thank you for helping.

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

#include "dictionary.h"

//introducing global variables/functions
int hash(const char *word)
{
    int hash = (toupper(word[0])-'A');
    return hash;
}
char word[LENGTH+1];
int dictionary_size = 0;

//defining nodes for linked lists
typedef struct node
{
    char* word;
    struct node* next;
} node;


//global variable linked list
node* first = NULL;

//introducing the hashtable
node* hashtable[26];

bool load(const char* dictionary)
{
    char word [LENGTH+1];
    // open file
    FILE* d = fopen("dictionary", "r");

    //check
    if (d == NULL)
    {
        printf("Could not open %s\n", dictionary);
        return 1;
    }
    else
    {
        for (int i = 0; i < 26; i++)
        hashtable[i] = NULL;
        for (int a = fgetc(d); a != EOF; a = fgetc(d))
        {
             //update dictionary size
             dictionary_size++;
            //create hashvalues
            int hashvalue = hash(word);
            //create node and scanf to word
            node* new_node = malloc(sizeof(node));
            strcpy(new_node -> word, word);
            //if hashvalue is the first
            if (hashtable[hashvalue] == NULL)
            {
                 hashtable[hashvalue] = new_node;
                 new_node -> next = NULL;
            }
            //if adding to linked list
            else
            {
                new_node -> next = hashtable[hashvalue];
                hashtable[hashvalue] = new_node;
            }
        }
    }

fclose(d);
return true;
}
1

FILE* d = fopen("dictionary", "r");

there it is^^ see it?

hint: you can´t fopen strings

edit: you may can but C doesn´t ;-)

You must log in to answer this question.

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