I get NULL from STRTOK?

1

Hello, I'm using strtok to split a string into words. My idea was to follow the word REMOVE deleted the next word from the text.

void algorithm(node **root, char *line){
    char *pch = strtok(line, " ");
    while(pch != NULL){
        if(strcmp(pch, "REMOVE")==0){
            pch = strtok(NULL, " ");
            if(pch == NULL){
                printf("error?");
            }
            removeNode(&(*root),pch);
        }else{
            insert(&(*root), pch);
        }
        pch = strtok(NULL, " ");
    }
}

Why is it that after pch = strtok(NULL, " "); always has pch = NULL ?

Note: I already tested with large files and a REMOVE there in the middle and the% w /% after this operation always gives% w /%, if the operation is at the end of the cycle the% w /% variable no longer% w /%.

    
asked by anonymous 09.03.2014 / 13:09

1 answer

0

I gave up strtok() and I'm reading the words one by one is much more efficient and I do not spend so much memory space.

while(scanf("%s ", word) > 0){
            if(removeNext){
                removeNode(&root, word);    
                removeNext = 0;
            }
            else if(strcmp(word, "REMOVE") != 0){
                i = 0;
                while(i < sizeof(word)){
                    word[i] = tolower(word[i]);
                    i++;
                }
                insert(&root, word);    
            }
            else if(strcmp(word, "REMOVE")==0){
                removeNext = 1;
            }
        }
    
25.03.2014 / 00:50