How can I use strtok for tokenize integers

Discussion in 'C Programming' started by Ram Laxman, Apr 11, 2004.

  1. Ram Laxman

    Ram Laxman Guest

    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
    Ram Laxman, Apr 11, 2004
    #1
    1. Advertising

  2. Ram Laxman <> 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "My absolute aspect is probably..."
    - Mato Valtonen
    Joona I Palaste, Apr 11, 2004
    #2
    1. Advertising

  3. Ram Laxman <> 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.
    Christopher Benson-Manica, Apr 12, 2004
    #3
  4. Ram Laxman

    Jack Klein Guest

    On Mon, 12 Apr 2004 02:35:36 +0000 (UTC), Christopher Benson-Manica
    <> wrote in comp.lang.c:

    > Ram Laxman <> 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/~ajo/docs/FAQ-acllc.html
    Jack Klein, Apr 12, 2004
    #4
  5. Ram Laxman

    Mac Guest

    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
    > <> wrote in comp.lang.c:
    >
    >> Ram Laxman <> 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
    Mac, Apr 12, 2004
    #5
  6. Ram Laxman

    CBFalconer Guest

    Christopher Benson-Manica wrote:
    > Ram Laxman <> 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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Apr 12, 2004
    #6
  7. Jack Klein <> 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.
    Christopher Benson-Manica, Apr 12, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lans
    Replies:
    9
    Views:
    478
    Chris \( Val \)
    Jul 10, 2003
  2. Kelvin@!!!

    tokenize a string

    Kelvin@!!!, Feb 24, 2005, in forum: C++
    Replies:
    4
    Views:
    7,895
  3. Andr? Roberge
    Replies:
    1
    Views:
    506
    Tim Peters
    Sep 10, 2004
  4. glomde
    Replies:
    3
    Views:
    291
    glomde
    May 9, 2006
  5. Replies:
    20
    Views:
    3,128
    Ben Bacarisse
    Feb 18, 2006
Loading...

Share This Page