0

enter image description hereAfter I compile dictionary.c, the dictionary.o and speller.o show up in the file holder. I doulbe click to open both files, and it shows the error:file format unsupported. Also, when I run ./speller texts/lalaland.txt, it shows could not open text. Can somebody help me, how I can fix it? Thank you.

// Default dictionary
#define DICTIONARY "dictionaries/large"


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

#define hashtable 26
//  26 buckets
node *bucket[hashtable]; //*

//prototype
int strcasecmp(const char *, const char *);

//hash function
int hash(const char* word);

int wordCount = 0;

 // Returns true if word is in dictionary else false
bool check(const char *word)
{

int num = hash(word);
node *cursor = bucket[num];

    while(cursor != NULL)
    {

        if (strcasecmp(word, cursor -> word ) == 0)
        {
            return true;
        }

    }

    // TODO
    return false;
}


// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    char word [LENGTH + 1];

    //open dictionary
    FILE *dicptr = fopen(dictionary, "r");
    if(dicptr == NULL)
    {
        return 1;
    }

    //scan dictionary word by word
    while(fscanf(dicptr, "%s\n", word) != EOF) //small dic or large dic? //%s\n,%c
    {

        // new word
        node *new_node = malloc(sizeof(node));


        if(new_node == NULL)
        {
            unload();
            return false;
        }
        else
        {
            //copy word into node
            strcpy(new_node -> word, word);
            wordCount ++;
            //initialize new node
            new_node -> next = NULL;
        }

        int number = hash(word);

        // if the hashtable is still empty
        if(bucket[number] == NULL)
        {
            bucket[number] = new_node;
            new_node -> next = NULL;
        }
        // if the hashtable already has word
        else
        {
            new_node -> next = bucket[0]; //->next
            bucket[number] = new_node;
        }

    }


    //todo
    return true;

}



int hash(const char* word)
{
    int index = 0;
    tolower(word[0]);
    index = word[0] - 'a';

    return index % hashtable;


}
2
  • What did you do to receive this error message? Please edit your question and include the commands leading to the error message. And make sure to follow the lines in the speller problem description, especially if compilation fails, try make clean and then make speller.
    – Blauelf
    Commented Apr 7, 2018 at 17:05
  • Please edit the question and add the details leading to this error. A screen capture of the terminal session with the commands and result would be good.
    – Cliff B
    Commented Apr 7, 2018 at 20:40

1 Answer 1

0

Easy issues to resolve. First, there is no executable called dictionary. It is an intermmediate build file (machine code). You have to execute ./speller as you did in the next command.

Next error - the file lalaland.txt doesn't exist because the directory text doesn't exist. It's misspelled and should be spelled with an s, texts/lalaland.txt (assuming that file actually exists.)

Now, once you resolve this, there is the problem of the infinite loop in check. It seems that the code keeps checking the same node without advancing to the next node in the linked list.

There are other issues, but if I told you, I'd be stealing all the fun you'd have debugging and developing those skills! ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thank you so much!
    – P.Lin
    Commented Apr 9, 2018 at 8:06

You must log in to answer this question.

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