I am completely stuck on pset5 load() function, have no idea where to go. Can someone please guide me in the right direction? Here is my code:

bool load(const char* dictionary) {
FILE* file = fopen(dictionary,"r");
if (file == NULL) {
    printf("Error. File is empty\n");
    return false; }
root = malloc(sizeof(node));
for (int i = 0; i < 27; i++) {
    root->children[i] = NULL; }
node* crawler = root;
int c;
size_t linecount = 0;
int charay[45];
char chars[45];
while((c = fgetc(file)) != EOF && linecount < 143099)
 //   printf("line : ");
   // printf("%c ", c);
     int index = tolower(c) - 'a';
     if (index >= 0 && index <= 27 ) {
     charay[linecount] = index;
     chars[linecount] = c;
     printf("c is %c\n",chars[linecount]);
     else  {
         for (int i = 0; i < 45; i++) {
             printf("charay linecount (%i) is %i\n, or letter %c\n",i,charay[i],chars[i]);

            while (charay[i] < 32698) { 
                node* new_node = malloc(sizeof(node));
                printf("i is %i\n",i);
              if (i == 0) {
               if (crawler->children[charay[i]] == NULL) {
                   printf("creating new_node with letter %c and index %i\n",chars[i],charay[i]);
                  crawler->children[charay[i]] = new_node; 

                 else {
                     printf("not creating node for letter  %c", chars[i]);
                     return true; }
            else {
                printf("i is not 0\n");

                    if (new_node->children[charay[i]] == NULL) { 
                        printf("creating 2new_node with letter %c and index %i\n",chars[i],charay[i]);
                        node* new_nodet = malloc(sizeof(node));
                      new_node->children[charay[i]] = new_nodet;
                    else {
                        printf("not2 creating node for letter %c\n",chars[i]);
            if (charay[i] < 0) { 
            crawler->is_word = true;
            for (int i = 0; i < 45; i++) {
                charay[i] = -1;
                chars[i] =  ' ';
                linecount = -1;
return true; }

So far, I have not worked on returning false if the dictionary has not loaded but just focused on actually loading the contents of the dictionary.


I tried to follow what you were doing, but couldn't quite make sense of all the pieces. It seems like the code is far more complicated than it needs to be. There's an old adage in programming,"KISS - Keep It Simple, Stupid!", something I tell myself at some point on almost every project. This code needs a major rewrite/redesign, there's no other way to say it. There's just too much that needs to be changed to delve into in this forum.

It looks like you're reading in each character in a word and saving them, until you get to the end of a line. Then it appears to be trying to add the word into the trie, one letter at a time. It also has a stray return that is causing load to exit after the first word is processed. Load() should only exit after the entire dictionary is processed.

Instead of the approach in the code, wouldn't it be easier to read a character from the dictionary file, then, if it's a letter or an apostrophe, add a new node, and if it's the end of a word, mark the end of the word and reset to the root node to start processing the next word?

A review of all of the class material for this pset would probably help too.

You must log in to answer this question.

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