0

I apologize but I am so lost. I have watched the videos over and over, read and reread the recommended material but am unable to figure this out. I hesitate to even post my code as it likely has so many flaws one wouldn't even know where to begin helping me.

I think my primary issue is not understanding how to get the info into my root or temp pointer. If using fgetc gets me to the location how do I grab the data that is there?

Thanks in advance.

bool load(const char* dictionary)
{
    FILE* fp = fopen(dictionary, "r");
    if(fp == NULL)
    {
        printf("Unable to open dictionary.\n");
        return false;
    }
    node* root = malloc(sizeof(node));
    node* trav = root;
    for (char c = fgetc(fp); c !=EOF; c = fgetc(fp))
    {
        for (int i = 0; i < LENGTH; i++)
        {
            if (c == '\n')
            {
                root->is_word = true;
                word_count++;
                return true;
            }
            if (c == EOF)
            {
                root->is_word = true;
                word_count++;
                break;
            }
            else
            {
                node* temp = malloc(sizeof(node));
                trav->child[i] = temp;
            }
        }
    }
    fclose(fp);
    return true;
}

EDIT: I have mode a few changes but am unsure how to make sure it's actually loading.

bool load(const char* dictionary)
{
    FILE* fp = fopen(dictionary, "r");
    if(fp == NULL)
    {
        printf("Unable to open dictionary.\n");
        return false;
    }
    root = calloc(1, sizeof(node));
    node* trav = root;
    int index = 0; 
    for (char c = fgetc(fp); c != EOF; c = fgetc(fp))
    {
        if (root->child[index] == NULL)
        {
            root->child[index] = calloc(1, sizeof(node));
        }
        else
        {
            if (c == '\'')
            {
                index = 26;
            }
            if (c == '\n')
            {
                trav->is_word = true;
                word_count++;
                trav = root;
            }
            if (trav->child[index] == NULL)
            {
                trav->child[index] = calloc(1, sizeof(node));
                trav = trav->child[index];
                index++;
            }
            else
            {
                trav = trav->child[index];
                index++;
            }
        }
    }
    fclose(fp);
    return true;
}

1 Answer 1

1

First, I would recommend using calloc for root node, because this one sets it to 0.

Then, in your first if statement, checking for '\n' does not necessarily mean that we are not pointing at an empty box. You are also setting words as true when is the EOF, but we should be counting words only when the '\n' is encountered and pointer not empty. You are loading the words, but make sure you handle chars and '\'' properly. What happens if the pointer is NULL? What should you do if it's not NULL?

Your logic is ok, you're close, but you need to make sure you deal with every char as we should.

Pointer points to the root node. Next, from there, your temp should point to the children of the node, i.e. to the children[c], but this c should be modified a bit - there's some math to be done here. You don't need the second loop, the fgetc one is sufficient enough.

HTH a bit!

2
  • I was assuming that when encountering "\n" it would mean I was at the end of a word.
    – Ja-ke-d
    Mar 10, 2016 at 19:45
  • Yes, but you need to make sure the pointer is not pointing at an empty box. So, in addition to checking to see the newline, you also need to check if trav is not NULL.
    – Lana
    Mar 11, 2016 at 7:05

You must log in to answer this question.

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