On 1 Dec 2006 07:43:45 -0800, "er****@student.chalmers.se"
<er****@student.chalmers.sewrote in comp.lang.c++:
On Dec 1, 4:27 pm, Christian Christmann <plfr...@yahoo.dewrote:
Hi,
what is the best approach to tokenize a "const char*"?
The strings look like "sometext 12345".
I need to read the number. The first sequence of characters
(here "sometext") is not important. Also the number of
white spaces is not known.
Iterate through the string and check the value of each char, if the
value is between 48 and 57 (inclusive) then it's a number. Next use the
atoi-function (from <cstdlib>) and pass the pointer to the first number
and out you get an int.
Don't ever recommend the use of atoi(), atol(), or atod() to anyone.
These are extremely unsafe functions, in that they produce undefined
behavior if the text string represents a value outside the range of
the type.
That's exactly why the C standard added strtol(), strtoul(), and
strtod() way back in 1989. They have defined behavior with any input.
And of course Gavin already mentioned the folly of using decimal
literals to specify digit characters.
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://c-faq.com/
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html