0
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include "dictionary.h"
#include "string.h"
#include <cs50.h>
#include <strings.h>
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

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

// Hash table
node *table[N];

//counter for how many words
int counter = 0;

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    long hashed = hash(word);
    for (node *tmp = table[hashed]; tmp != NULL; tmp = tmp->next)
    {
        if (strcasecmp(tmp->word, word) == 0)
        {
            return true;
        }
    }
    // TODO
    return false;
}

// Hashes word to a number
// djb2 hash function
unsigned long hash(const char *word)
{
    unsigned long hash = 5381;
    int c;

    while ((c = *word++))
        hash = ((hash << 5) + hash) + c;

   return hash % N;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        return false;
    }
    //read each individual word into this
    char word[LENGTH+1];

    // scan individual words one at a time till we reach EndOfFile ie EOF
    while(fscanf(file, "%s", word) != EOF)
    {
        //malloc space for node
        node *n = malloc(sizeof(node));

        int index = hash(word);

        if (n == NULL)
        {
            printf("memory error");
            return false;
        }
        else
        {
            //cant copy an array using the assignment operator, had to use strcpy
            strcpy(n->word, word);

            n->next = table[index];
            table[index] = n;
            counter++;
        }
    }
    fclose(file);
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    //printf("Number of words in the Dictionary: %i\n", counter);
    return counter;
}

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

the compiler gives me this but couldnt seem to figure out where im messing up


Could not open texts/austinpowers.txt.


Segmentation fault

3
  • The first problem is simple. When the program executes, it can't find the test file austinpowers.txt. Is the subdirectory texts in the current directory and does the text file exist (and spelled correctly) in the subdirectory? Next is a bigger problem. There's a seg fault. You need to identify which line is causing the seg fault. Please read this: cs50.stackexchange.com/q/37065/4847 – Cliff B Apr 30 '20 at 21:21
  • thanks for this, im exhausted and im making mistakes – cook Apr 30 '20 at 21:49
  • Then you should take a break! Put it aside for a day or even two. Don't think about it at all. It'll give your subconscious a chance to digest everything and make more sense of it. When you come back, you'll be surprised at how much clearer and easier it will be! This is serious advice!!!! It happens to me too, from time to time. – Cliff B Apr 30 '20 at 22:30

You must log in to answer this question.

Browse other questions tagged .