Tokenizing Strings
I need to tokenize strings!
I''ve used strtok() but it crashes when I tell it to look for the second token.... This is very annoying.
Are there any alternatives, or will I have to write my own function?
If you''re making a script parser, I''d write your own, you''ll have more control over how it is tokenized. eg. operators like +-*/ should be separated even if there isn''t a space between the variable.
If you''re just trying to extract data from a string separated by spaces, then there is some function which is like the reverse of sprintf, but I can''t remember what it''s called. sorry, that''s not very helpful, but what the hell
Frank
If you''re just trying to extract data from a string separated by spaces, then there is some function which is like the reverse of sprintf, but I can''t remember what it''s called. sorry, that''s not very helpful, but what the hell
Frank
June 30, 2001 01:41 PM
Strtok() sucks ass because it modifies/destroys your source string.
Keep that in mind. It screwed me up big time when I wrote a parser for a little synth I tried to make (using text files for input). Anyway, I don''t remember exactly what happened, but I ended up having to make a copy of my source string, and passing that to strtok().
Strtok is very useful, but, as the AP said, you will need a copy of the source string, otherwise it gets mangled. Notice the "char seps[]", in which you can place anything that you want, such as ''+'' or ''-'' or '' '' etc etc etc. The following code is taken from my console. It isn''t the most versatile, being designed specifically for my needs, but it works a treat.
I think the formatting may get screwed around with the comments, but I''m not sure...
Anyway, cheers
|
I think the formatting may get screwed around with the comments, but I''m not sure...
Anyway, cheers
If you need to write your own func, you could do it fairly easily, especially if you can hard-code the seperator...
something I just made up... not tested![](smile.gif)
...instr is the string to tokenize, outstr is a buffer for the token, start is where to start at (init any var to 0 and keep passing the same var w/o modifying it's value)...
edit: btw, if you need more seps, just add " || *instr = ','", etc to the while..
--Tr][aD--
Edited by - TrIaD on June 30, 2001 10:09:03 PM
something I just made up... not tested
![](smile.gif)
|
...instr is the string to tokenize, outstr is a buffer for the token, start is where to start at (init any var to 0 and keep passing the same var w/o modifying it's value)...
edit: btw, if you need more seps, just add " || *instr = ','", etc to the while..
--Tr][aD--
Edited by - TrIaD on June 30, 2001 10:09:03 PM
--Tr][aD--
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement