0

I have been stuck with the load and check functions for weeks. I have already finished the unload and count functions but the first don't seem to work. I tried using the gdb and I think that the problem is here:

My "word" array is displayed as follows when I implement info locals after assigning to word the first character of the first word in dictionaries/large:

"\177ELF\002\001\001\000\000\000\000\000\000\000\000\000\002\000>\000\001\000\000\000\200\a@\000\000\000\000\000@\000\000\000\000\000\000\000\210?\000\000\000"

So my code cannot read the word as it should, skipping through the first four characters (being nor letters nor apostrophes).

Can anyone help me? I have tried with fgets, fgetc, fscanf and the same problem arises.

I'm reposting a post I posted beforehand to gain visibility since I have not had an an answer for some time.

Thanks in advance!

Here is my load function:

/** * Loads dictionary into memory. Returns true if successful else false. / bool load(const char dictionary) { // allocates and defines root root = (struct node*) calloc (1, sizeof(node)); // allocates and defines trav (crawler) node* trav = calloc(1, sizeof(node)); if (trav == NULL) { printf("No space in memory\n"); return false; } trav = root; char word[46]; // naming the index for the character in the word int i = 0; // declares the position of the letter in the array int pos = 0; // opens the file FILE* dict = fopen(dictionary, "r"); if (dict == NULL) { printf("Error loading dictionary"); return false; } while (fgets(word, LENGTH + 1, dict) != NULL) { // iterates through the word array creating the necessary nodes while(isalpha(word[i]) || word[i] == '\'') {
char c = word[i]; // names an integer from 0 to 26 for each character if (isalpha(c)) {
pos = c - 'a'; } else if(c == '\'') { pos = 26; } // if the pointer to the character doesn't exist it is created if (trav->children[pos] == NULL) {
trav->children[pos] = (struct node*) calloc (1, sizeof(node)); } trav = trav->children[pos]; i++; } // sets is_word to true for the last letter of the word trav->is_word = true; count++; // sets the trav to point to root for a new word trav = root; }
if (feof(dict) == true) { return true; } else { return false; } }

Edit#1

bool load(const char* dictionary) { // allocates and defines root root = (struct node*) calloc (1, sizeof(node)); // allocates and defines trav (crawler) node* trav = calloc(1, sizeof(node)); if (trav == NULL) { printf("No space in memory\n"); return false; } trav = root; // creates an array to store the word being read char word[46]; // declares an index for the character in the word being read int i = 0; // declares the position of the letter in the array int pos = 0; // opens the file FILE* dict = fopen(dictionary, "r"); if (dict == NULL) { printf("Error loading dictionary"); return false; } while (fgets(word, LENGTH + 1, dict) != NULL) { // iterates through the word array creating the necessary nodes while(isalpha(word[i]) || word[i] == '\'') {
char c = word[i]; // names an integer from 0 to 26 for each character if (isalpha(c)) {
pos = c - 'a'; } else if(c == '\'') { pos = 26; } // if the pointer to the character doesn't exist it is created if (trav->children[pos] == NULL) {
trav->children[pos] = (struct node*) calloc (1, sizeof(node)); } trav = trav->children[pos]; // moves the indexes of both the file and the word array being read i++; } // sets is_word to true for the last letter of the word trav->is_word = true; count++; // sets the trav to point to root for a new word trav = root; // resets the index of the word array i = 0; }
if (feof(dict) == true) { return true; } else { return false; } }

1

First, there's a problem with your load function signature, but I'll assume that the missing * in bool load(const char* dictionary) at the top is just a typo.

The main problem is a small error of omission buried deep inside the load function, but with a very big impact. After you finish putting the first word in the dictionary, and start the second word, what are you using for the first letter? What is word[i]? More specifically, what is i???? If you think it is zero, you need to manually walk through your code, or insert a printf statement to see what it actually does.

That should get you going again. There may be other problems, but you should try to work them out on your own first. :-)

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

1
  • I understand what you say Cliff and I have corrected the index bug in EDIT #1. However, I still cannot understand why my word array is showing a 177ELF like a first character! This is not even affected by the index problem given that it is the first character in the word array. – Lesscomfortable Feb 25 '16 at 17:51

You must log in to answer this question.

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