0

I have implemented this for load. But if I execute the speller file in terminal, I get segmentation fault. I haven't implemented anything except for load as of now.

here it is:

 //Trie Data Structure.
typedef struct node
{
    bool isWord;
    struct node* children[27];
}node;

//Make a new node function prototype.
node* newNode();

//Declaring it here so that the root is global. and all other global variables
char word[45+1];
node* root = NULL;
int length;
int c;
unsigned int index;
FILE* dictfp = NULL;

/**
 * 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)
{
    //root is initailzed as a node.
    root = newNode();
    root->isWord = false;
    length = strlen(dictionary);

    //Open the dictionary
    dictfp = fopen(dictionary, "r");
    //return null if file can't be opened.
    if (dictfp == NULL)
    {
        return false;
    }
    //a traversal pointer equal to root. We'll use only this traversal pointer.
    node* trav = root;

    //loop to iterate through all the words in the dictionary file.
    while(!feof(dictfp))
    {
        fscanf(dictfp, "%s\n", word);
        length = strlen(word);

                for (int i = 0; i < length; i++)
                {
                    c = word[i];
                    index = tolower(c) - 'a';

                    if (trav->children[index] == NULL)
                    {
                        node* newnode = newNode();
                        trav->children[index] = newnode;

                    }else if (trav->children[index] != NULL)
                    {
                     trav = trav->children[index];   
                    }
                    if (c == '\n')
                    {
                        trav->isWord = true;
                    }
                }
    }

    fclose(dictfp);

    return true;
}

/**
 * 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;
}




////Make a new node function
node* newNode()
{
    node* newnode = calloc(1, sizeof(node));
    //setting all children pointers to NULL.
    for (int i = 0; i < 27; i++)
    {
        newnode->children[i] = NULL;
    }
    return newnode;
}

EDIT: I get "Could not open cat." in terminal if I use the small dictionary and cat as input. Could it be because of only having implemented Load so far.

1

Your code doesn't handle apostrophes. If the dictionary file has a word with an apostrophe, the index value generated is -58, so trav->children[index] generates a seg fault.

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

5
  • Ahh! Of course. I'll be back at my Mac in an hour or so and will give it a try. So hang on! Thank you! Is there anything else I am missing? – yousafe007 Apr 16 '16 at 18:46
  • I fixed that, But I get this at the terminal: could not open hey. (if the text entered was hey). What should I do? – yousafe007 Apr 16 '16 at 20:26
  • the way you're saying this makes me think that you're trying to pass the actual text as a parameter. The program is expecting a file name, not actual text to check. The program is going to look for a file with that name, and the file must exist in the current directory (or the path/filename location that you give). – Cliff B Apr 16 '16 at 20:55
  • Another thing that doesn't look right is the fact that you define word as an array of size 46. You then read in 46 letters from the file. You need to figure out a way to stop when you reach a linefeed. – Brendan Rafferty Apr 18 '16 at 18:05
  • @CliffB Can you answer how to resolve this segmentation error? cs50.stackexchange.com/questions/24795/… – Benedict Florance May 7 '17 at 14:51

You must log in to answer this question.

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