c - Problem with strtok only saving the first token -


I had a problem with my work yesterday, which was a simple fix, and now I have a different problem It is bothering me with this function a token effect:

  zero tokenizer (file * file, straight set * set) {int nbytes = 100; Int bits; Four * buffer; Four * tokens; Buffer = (four *) młok (nbytes + 1); While ((bytesread = gateline (& amp; buffer, & amp; nbytes, file)) = -1) {token = strtok (buffer, ""); While (token! = Null) {add (set, token); Token = strotok (NULL, ""); }}}   

I know that input (a text file) is correctly broken in the token because in the second loop I can display the printf ("% s", token) For each token, however, with the problem adding, it is only adding the first token to my list, but at the same time still every token is broken correctly. For example, if my input text was "Blow Herp Deckup", then I would like to

Token = Blah

Token = Hep

Token = derp < / P> will get

but only the first token in the list, will be blah. I do not believe this problem is with adding because it works on itself, that is, I can use

  plus (set, "blah"); (Set, "herp"); (Set, "Derp");   

And the result will be a list containing all three words.

Thanks for any help!

Stroke () gives an indicator in the string buffer. You need strdup () of that code and instead add results to your tree. Do not forget to free () when you clean the tree.

Comments