473,385 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How can I use strtok for tokenize integers

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
Nov 14 '05 #1
6 3948
Ram Laxman <ra********@india.com> scribbled the following:
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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"My absolute aspect is probably..."
- Mato Valtonen
Nov 14 '05 #2
Ram Laxman <ra********@india.com> spoke thus:
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 );

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #3
On Mon, 12 Apr 2004 02:35:36 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote in comp.lang.c:
Ram Laxman <ra********@india.com> spoke thus:
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>.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
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
Nov 14 '05 #4
Mac
On Sun, 11 Apr 2004 22:29:56 +0000, Jack Klein wrote:
On Mon, 12 Apr 2004 02:35:36 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote in comp.lang.c:
Ram Laxman <ra********@india.com> spoke thus:
> 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

Nov 14 '05 #5
Christopher Benson-Manica wrote:
Ram Laxman <ra********@india.com> spoke thus:
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 */
}

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #6
Jack Klein <ja*******@spamcop.net> spoke thus:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ram Laxman | last post by:
Hi all, I have written the following code: /* strtok example */ #include <stdio.h> #include <string.h> static const char * const resultFileName = "param.txt";
6
by: Ram Laxman | last post by:
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:...
3
by: New | last post by:
I have a String from a file in the form <number> <word>,<word>,<word>,<word> <number> when I try to tokenize it I store it in a char and tokenize it using strtok(string, " ") and then tokenize the...
20
by: bubunia2000 | last post by:
Hi all, I heard that strtok is not thread safe. So I want to write a sample program which will tokenize string without using strtok. Can I get a sample source code for the same. For exp:...
8
by: manochavishal | last post by:
Hi I am writing a Program in which i get input as #C1012,S,A#C1013,S,U I want to get C1012,S,A using strtok and then pass this to function CreateVideo which will further strtok this...
2
by: manochavishal | last post by:
Hi I am writing a Program in which i get input as #C1012,S,A#C1013,S,U I want to get C1012,S,A using strtok and then pass this to function CreateCopies which will further strtok this...
18
by: Robbie Hatley | last post by:
A couple of days ago I dedecided to force myself to really learn exactly what "strtok" does, and how to use it. I figured I'd just look it up in some book and that would be that. I figured...
75
by: siddhu | last post by:
Dear experts, As I know strtok_r is re-entrant version of strtok. strtok_r() is called with s1(lets say) as its first parameter. Remaining tokens from s1 are obtained by calling strtok_r() with...
12
by: Pilcrow | last post by:
Here is a quick program, together with its output, that illustrates what I consider to be a deficiency of the standard function strtok from <string.h>: I am using C:\>gcc --version gcc (GCC)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.