0

I commented out my check function so it must be something here in load. From what I've read I think the issue is dealing with pointers and fgetc. That or my first for loop is looping weird. granted I'm feeling loopy looking at this for so long.. Any tips? thanks!

bool load(const char* dictionary)
{

file = fopen ("dictionary/large.text", "r");

root = malloc(sizeof(node));

trav = root;

for (int c = fgetc(file); c != EOF; c = fgetc(file))
{
    if (isalpha(c) || c == '\'')
    {
        c = tolower(c) - 97;
        if (c == '\'')
        {
            c = 26;
        }
        if (trav->children[c] == NULL)
        {
            trav->children[c] = malloc(sizeof(node));
            trav = trav->children[c];
        }
        else
        {
            trav = trav->children[c];
        }

    }
    else
    {
        trav->is_word = true;
        trav = root;
    }
}

fclose(file);

return false;

}

1
  1. DinoCoderSaurus is right, you should check any file pointer after fopen for being NULL (that's your current segfault, and use fopen (dictionary, "r");, not a hard-coded path). In theory, that's also true for malloc, to maybe exit gracefully instead of segfaulting. Doesn't always help, but you should still check it.

  2. malloc does not initialize the memory, so the structure has arbitrary content. Either initialize after malloc, or use calloc instead, which zeroes the memory block it allocated, turning all pointers NULL and booleans false.

  3. Put the c = tolower(c) - 97; in the else path of if (c == '\''), as c no longer is '\'' otherwise, since you just subtracted 'a'.

  4. At the end, you return false, for "load failed". Maybe change that to true?

2
  • I switched malloc to root = calloc(1, sizeof(node)); but i don't understand the "1" – Nathan Pierce Feb 16 '17 at 17:27
  • It's funny after posting original questions I was able to find these very 4 things so its very validating in regards to my problem solving skills. It just sucks it takes hours of pulling hair out / crying then submitting to asking for online help, only to not really have to waste other peoples time in the first place :P nonethelese thanks!! – Nathan Pierce Feb 16 '17 at 17:33
1

Attention to detail. What happens if "dictionary/large.text" doesn't exist? (and in a standard installation, it doesn't!). Boom, seg fault. Don't forget to test for success.

function takes dictionary as an argument, so don't open a "named" file.

3
  • I think I used fgetc, about that way, it works well. – Blauelf Feb 16 '17 at 16:42
  • lol yes! about 5 mins ago i found that it should just be ' file = fopen (dictionary, "r") ' considering thats how its defined in .h file. Also now it loads just fine AND it is able to work with fgetc as far as i can tell... – Nathan Pierce Feb 16 '17 at 17:22
  • DOH! the isalpha filters out the \n. Answer emended. – DinoCoderSaurus Feb 16 '17 at 17:45
0

Made these changes. Thanks!

file = fopen (dictionary, "r");

as its already defined in .h file

root = calloc(1, sizeof(node));

because reasons... not sure i fully grasp this concept or the syntax "1" in the code

c = tolower(c) - 97;

moved

    return true;

makes sense

thanks people!

3
  • Also trav->children[c] = calloc(1, sizeof(node));? – Blauelf Feb 17 '17 at 13:09
  • Can do, thank you! the pseudo they given in the problem set tells us to malloc so i just went with that... – Nathan Pierce Feb 17 '17 at 16:08
  • malloc is fine if you actually do the initialisation. In this case, that's equivalent to zeroing all the fields, which calloc conveniently does. – Blauelf Feb 17 '17 at 16:28

You must log in to answer this question.

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