开发者

Problem with strtok only saving the first token

I was having a problem with my function yesterday that turned out to be a simple fix, and now I am have a different issue with it that is baffling me. The function is a tokenizer:

void tokenizer(FILE *file, struct Set *set) {
     int nbytes = 100;
     int bytesread;
     char *buffer;
     char *token;
     buffer = (char *) malloc(nbytes + 1);
     while((bytesread = getLine(&buffer,&nbytes,file)) != -1) {
          token = strtok(buffer," ");
          while(token != NULL) 开发者_如何学编程{
              add(set,token);
              token = strtok(NULL," ");
          }
     }
}

I know that the input(a text file) is getting broken into tokens correctly because in the second while loop I can add printf("%s",token) to display each token. However, the problem is with add. It is only adding the first token to my list, but at the same time still breaks every token down correctly. For example, if my input text was "blah herp derp" I would get

token = blah

token = herp

token = derp

but the list would only contain the first token, blah. I don't believe that the problem is with add because it works on its own, ie I could use

add(set,"blah");
add(set,"herp");
add(set,"derp");

and the result will be a list that contains all three words.

Thank you for any help!


strtok() returns a pointer into the string buffer. You need to strdup() that string and add the result to your tree instead. Don't forget to free() it when you clean up the tree.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜