LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   insert() in trie with erroneous count (https://www.linuxquestions.org/questions/programming-9/insert-in-trie-with-erroneous-count-4175498226/)

atlantis43 03-14-2014 04:32 PM

insert() in trie with erroneous count
 
I'm missing something in the following code, which is providing a counter that is +1 from correct count when I enter a pre-provided "dictionary" of 143091 entries, but which enters the correct count if I enter a simple, self written "dictionary" of 10 entries. Am I missing something related to EOF or something of that sort?
The calling code is:
Code:

word = malloc(LENGTH);
while(fgets(word,LENGTH,fp))
    {
    len= strlen (word);
    if (len>0 && word[len-1]=='\n') word[--len]= '\0';
                                                       
    insert (&trie, word);

And the insert() function is as follows, where d_cnt is the # of entries:
Code:

void insert(trie_t *pTrie, char key[])
{
    //declarations
    int level; 
    int length = strlen(key);
    int index;
    trie_node_t *pCrawl;
    ptrTrie = pTrie;       
      pCrawl = pTrie->root;
     
    for( level = 0; level < length; level++ ) 
    {                                             
        if(CHAR_TO_INDEX(key[level])>=0)
        {
        index = CHAR_TO_INDEX(key[level]);
        }
        if(CHAR_TO_INDEX(key[level])<0)
        {
        index=26;
        }
       
        if( !pCrawl->children[index] )     
       
        {
            pCrawl->children[index] = getNode();//ADDS ANOTHER LEVEL OF NODES
        }
 
        pCrawl = pCrawl->children[index];
    }
    // mark last node as leaf
    pCrawl->value = pTrie->dSize;
    dSize++;
}

Thanks for your attention.
richard

atlantis43 03-14-2014 10:12 PM

disregard additional info previously posted here.

NevemTeve 03-15-2014 12:37 AM

There might be empty lines or lines that are longer than LENGTH.

Code:

word = malloc(LENGTH);
while(fgets(word,LENGTH,fp)) {
    ++nline;
    len= strlen (word);
    if (len>0 && word[len-1]=='\n') {
        word[--len]= '\0';
    } else {
        fprintf (stderr, "*** Problematic line #%ld\n", (long)nline);
    }                                               
    if (len==0) continue;
    insert (&trie, word);


atlantis43 03-15-2014 07:47 AM

That was, indeed, exactly correct regarding the problem with my code. Thanks again for your help.


All times are GMT -5. The time now is 07:13 PM.