How can I use strtok for tokenize integers

R

Ram Laxman

Hi all,
How can I tokenize the integers using strtok. For example:
If I have some thing like:
"ram":"laxman":"deepak"

then I can safely use strtok.But if I have something like below:
10:20:50:79

How can I use strtok to tokenize based on delimiter(In this case --> : )?
10
20


Can anybody help me in this regard?

Regards
Ram Laxman
 
J

Joona I Palaste

Ram Laxman said:
Hi all,
How can I tokenize the integers using strtok. For example:
If I have some thing like:
"ram":"laxman":"deepak"
then I can safely use strtok.But if I have something like below:
10:20:50:79
How can I use strtok to tokenize based on delimiter(In this case --> : )?
10
20
Can anybody help me in this regard?

Tokenise them as strings first, like you're doing now, then convert
each token with strtol or strtod.
 
C

Christopher Benson-Manica

Ram Laxman said:
then I can safely use strtok.But if I have something like below:
10:20:50:79
How can I use strtok to tokenize based on delimiter(In this case --> : )?
10
20

I'm not sure what you're trying to do, but I doubt strtok is a good
way to go about it. If you're just trying to parse a file (or string)
into a structure containing integers, *scanf() can make your life much
easier:

char my_str[] = "10:20:50:79";
int x1, x2, x3, x4;

sscanf( my_str, "%d:%d:%d:%d", &x1, &x2, &x3, &x4 );
 
J

Jack Klein

Ram Laxman said:
then I can safely use strtok.But if I have something like below:
10:20:50:79
How can I use strtok to tokenize based on delimiter(In this case --> : )?
10
20

I'm not sure what you're trying to do, but I doubt strtok is a good
way to go about it. If you're just trying to parse a file (or string)
into a structure containing integers, *scanf() can make your life much
easier:

char my_str[] = "10:20:50:79";
int x1, x2, x3, x4;

sscanf( my_str, "%d:%d:%d:%d", &x1, &x2, &x3, &x4 );

Members of the *scanf() family are not good things to recommend. If
text converts to a value outside the range of the destination type,
they produce undefined behavior, just as the ato*() family do. And if
you are going to pre-check the string to verify that the values are in
range, you might as well just convert them yourself in the process.

The only text to numeric conversion functions in the C standard
library that have completely defined behavior no matter what the input
are the strto*() functions from <stdlib.h>.
 
M

Mac

Ram Laxman said:
then I can safely use strtok.But if I have something like below:
10:20:50:79
How can I use strtok to tokenize based on delimiter(In this case --> : )?
10
20

I'm not sure what you're trying to do, but I doubt strtok is a good
way to go about it. If you're just trying to parse a file (or string)
into a structure containing integers, *scanf() can make your life much
easier:

char my_str[] = "10:20:50:79";
int x1, x2, x3, x4;

sscanf( my_str, "%d:%d:%d:%d", &x1, &x2, &x3, &x4 );

Members of the *scanf() family are not good things to recommend. If
text converts to a value outside the range of the destination type,
they produce undefined behavior, just as the ato*() family do. And if
you are going to pre-check the string to verify that the values are in
range, you might as well just convert them yourself in the process.

The only text to numeric conversion functions in the C standard
library that have completely defined behavior no matter what the input
are the strto*() functions from <stdlib.h>.


In this case, I think it might be OK to use sscanf() but read the numbers
in as strings, using field lengths to be safe. Then the strings can be
safely converted to numbers using strtol() or whatever is appropriate.

--Mac
 
C

CBFalconer

Christopher said:
Ram Laxman said:
then I can safely use strtok.But if I have something like below:
10:20:50:79
How can I use strtok to tokenize based on delimiter(In this case --> : )?
10
20

I'm not sure what you're trying to do, but I doubt strtok is a
good way to go about it. If you're just trying to parse a file
(or string) into a structure containing integers, *scanf() can
make your life much easier:

char my_str[] = "10:20:50:79";
int x1, x2, x3, x4;

sscanf( my_str, "%d:%d:%d:%d", &x1, &x2, &x3, &x4 );

Never ignore the return values. Try:

if (4 != (err = sscanf(my_str, "%d:%d:%d:%d",
&x1, &x2, &x3, &x4))) {
/* error recovery games */
}
else {
/* successful scan, use the data */
}
 
C

Christopher Benson-Manica

Jack Klein said:
Members of the *scanf() family are not good things to recommend.

Neither is fgets(), if Dan is to be believed. In OP's case, at least,
the situation looks simple enought that overflow issues won't be a
problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top