strtok problem

Discussion in 'C++' started by plmanikandan@gmail.com, Mar 29, 2006.

  1. Guest

    Hi,
    I need to split the value stored in a string and store them to another
    charrecter array.I am using strtok function.But i am getting invalid
    output when there is no value between delimiter

    my code
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    void main()
    {

    char *pch;
    char *parameters[4];
    int paramcount=0;

    char b[]="mani,raju,musa,kumar";

    pch=strtok(b,",");
    while (pch != NULL)
    {
    //parameters[paramcount]=pch;
    parameters[paramcount]=(char*)malloc(strlen(pch) + 1);
    strcpy(parameters[paramcount],pch);
    paramcount++;
    pch = strtok (NULL, ",");
    }//while (pch != NULL)

    for(int t=0;t<4;t++)
    printf("\n%s\n",parameters[t]);

    }
    the above code works fine.when i change the input as follows
    char b[]="mani,,,kumar"
    The output is not correct.
    How to free the memory allocated?
    how to handle strtok when there is no value between delimiter

    Regards,
    Manikandan
     
    , Mar 29, 2006
    #1
    1. Advertising

  2. Puppet_Sock Guest

    wrote:
    [snip]
    > pch=strtok(b,",");

    [snip]
    > the above code works fine.when i change the input as follows
    > char b[]="mani,,,kumar"
    > The output is not correct.
    > How to free the memory allocated?
    > how to handle strtok when there is no value between delimiter


    I am convinced that the main purpose of the strtok function being
    in the library is to catch new coders in the knees.

    Carefully read up on the strtok function in your compiler's docs.
    Note what happens when there are multiple delimeters with
    nothing between them. So, your "not working" input will be
    equivalent to

    char b[]="mani,kumar"

    that is, there's no way for you to use strtok to get those missing
    fields in the middle.

    Also, read up in your docs about the various warnings and caveats
    about using strtok. The security warning that should be there, for
    example. And the warning about a static variable being used so that
    multiple overlapping calls with different strings to tokenize will
    interfere with eachother.

    My opinion: Don't use strtok. Roll up your own parsing class,
    preferably using std::string instead of char arrays. But even if
    you can't use std::string, drop the use of strtok.

    Write yourself a class that takes your input, copies it to an
    internal buffer, then parses it out to a vector of tokens. Give
    it the behaviour you need it to have with regard to missing
    entries. Make sure you follow all the "motherhood" rules
    about class design such as the rule of 3, and so on. Then
    give it a small number of "get" funcs that pull out the number
    of items in the vector, and the "nth" item in the list. You
    can then have a really good time deciding what to do about
    such things as asking for an element past the end (the
    7th element when there are only 4, as an example.)

    You can even have such things as multiple delimeters,
    delims that are more than one char, even calculated delims
    that depend on rules such as a space after a period is a
    delim, but a space after anything else is not.
    Socks
     
    Puppet_Sock, Mar 29, 2006
    #2
    1. Advertising

  3. Pete Becker Guest

    wrote:

    > the above code works fine.when i change the input as follows
    > char b[]="mani,,,kumar"
    > The output is not correct.


    Well, that depends on what "correct" means. According to the definition
    of strotk, the new input has two data fields, separated by three commas.
    strtok swallows all consecutive occurrences of characters in the
    delimiter string.

    > How to free the memory allocated?


    Same way you allocated it: step through the array and call delete[] on
    each allocated block.

    > how to handle strtok when there is no value between delimiter
    >


    strtok doesn't do that. You can use strcspn if you don't mind doing a
    little more bookkeeping. Or you can use the STL algorithm find_first_of
    to find the next occurrence of the delimiter in a C string. Or, at the
    cost of a little more overhead, copy the text into a C++ string object,
    and use its member function find_first_of.

    --

    Pete Becker
    Roundhouse Consulting, Ltd.
     
    Pete Becker, Mar 29, 2006
    #3
  4. Sgt. York Guest

    Puppet_Sock wrote:
    > wrote:
    > [snip]
    >> pch=strtok(b,",");

    > [snip]
    >> the above code works fine.when i change the input as follows
    >> char b[]="mani,,,kumar"
    >> The output is not correct.
    >> How to free the memory allocated?
    >> how to handle strtok when there is no value between delimiter

    >
    > I am convinced that the main purpose of the strtok function being
    > in the library is to catch new coders in the knees.
    >
    > Carefully read up on the strtok function in your compiler's docs.
    > Note what happens when there are multiple delimeters with
    > nothing between them. So, your "not working" input will be
    > equivalent to
    >
    > char b[]="mani,kumar"
    >
    > that is, there's no way for you to use strtok to get those missing
    > fields in the middle.
    >
    > Also, read up in your docs about the various warnings and caveats
    > about using strtok. The security warning that should be there, for
    > example. And the warning about a static variable being used so that
    > multiple overlapping calls with different strings to tokenize will
    > interfere with eachother.
    >
    > My opinion: Don't use strtok. Roll up your own parsing class,
    > preferably using std::string instead of char arrays. But even if
    > you can't use std::string, drop the use of strtok.
    >
    > Write yourself a class that takes your input, copies it to an
    > internal buffer, then parses it out to a vector of tokens. Give
    > it the behaviour you need it to have with regard to missing
    > entries. Make sure you follow all the "motherhood" rules
    > about class design such as the rule of 3, and so on. Then
    > give it a small number of "get" funcs that pull out the number
    > of items in the vector, and the "nth" item in the list. You
    > can then have a really good time deciding what to do about
    > such things as asking for an element past the end (the
    > 7th element when there are only 4, as an example.)
    >
    > You can even have such things as multiple delimeters,
    > delims that are more than one char, even calculated delims
    > that depend on rules such as a space after a period is a
    > delim, but a space after anything else is not.
    > Socks
    >


    Better yet, use John Bandela's Boost tokenizer (www.boost.org). From
    there, you'll likely discover other boost delicacies that will make the
    library almost indispensable.

    -York
     
    Sgt. York, Mar 29, 2006
    #4
  5. Daniel T. Guest

    In article <>,
    wrote:

    > Hi,
    > I need to split the value stored in a string and store them to another
    > charrecter array.I am using strtok function.But i am getting invalid
    > output when there is no value between delimiter
    >
    > my code
    > #include<stdio.h>
    > #include<string.h>
    > #include<stdlib.h>
    > void main()
    > {
    >
    > char *pch;
    > char *parameters[4];
    > int paramcount=0;
    >
    > char b[]="mani,raju,musa,kumar";
    >
    > pch=strtok(b,",");
    > while (pch != NULL)
    > {
    > //parameters[paramcount]=pch;
    > parameters[paramcount]=(char*)malloc(strlen(pch) + 1);
    > strcpy(parameters[paramcount],pch);
    > paramcount++;
    > pch = strtok (NULL, ",");
    > }//while (pch != NULL)
    >
    > for(int t=0;t<4;t++)
    > printf("\n%s\n",parameters[t]);
    >
    > }
    > the above code works fine.when i change the input as follows
    > char b[]="mani,,,kumar"
    > The output is not correct.
    > How to free the memory allocated?
    > how to handle strtok when there is no value between delimiter


    I'm thinking your confused as accidentally posted this in a C++
    newsgroup instead of a C newsgroup. Could that be the case? If not...

    I suggest you use some standard C++ components:

    #include <algorithm> // for copy & find
    #include <iostream> // for cout
    #include <iterator> // for ostream_iterator
    #include <string>
    #include <vector>

    using namespace std;

    int main()
    {
    //char b[]="mani,raju,musa,kumar";
    char b[]="mani,,,kumar";

    vector<string> vec;
    const char* first = b;
    const char* last = b + strlen( b );
    while ( first != last ) {
    const char* next = find( first, last, ',' );
    vec.push_back( string( first, next - first ) );
    first = min( next + 1, last );
    }
    copy( vec.begin(), vec.end(),
    ostream_iterator<string>( cout, "\n" ) );
    }


    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
     
    Daniel T., Mar 30, 2006
    #5
  6. Daniel T. Guest

    In articl
    <>
    wrote

    > Hi
    > I need to split the value stored in a string and store them t

    anothe
    > charrecter array.I am using strtok function.But i am getting invali
    > output when there is no value between delimite
    >
    > my cod
    > #include<stdio.
    > #include<string.
    > #include<stdlib.
    > void main(
    >
    >
    > char *pch
    > char *parameters[4]
    > int paramcount=0
    >
    > char b[]="mani,raju,musa,kumar"
    >
    > pch=strtok(b,",")
    > while (pch != NULL
    >
    > //parameters[paramcount]=pch
    > parameters[paramcount]=(char*)malloc(strlen(pch) + 1)
    > strcpy(parameters[paramcount],pch)
    > paramcount++
    > pch = strtok (NULL, ",")
    > }//while (pch != NULL
    >
    > for(int t=0;t<4;t++
    > printf("\n%s\n",parameters[t])
    >
    >
    > the above code works fine.when i change the input as follow
    > char b[]="mani,,,kumar
    > The output is not correct
    > How to free the memory allocated
    > how to handle strtok when there is no value between delimite
    >

    I'm thinking your confused as accidentally posted this in a C++
    newsgroup instead of a C newsgroup. Could that be the case? If not..

    I suggest you use some standard C++ components

    #include <algorithm> // for copy & fin
    #include <iostream> // for cou
    #include <iterator> // for ostream_iterato
    #include <string
    #include <vector

    using namespace std

    int main(

    //char b[]="mani,raju,musa,kumar"
    char b[]="mani,,,kumar"

    vector<string> vec
    const char* first = b
    const char* last = b + strlen( b )
    while ( first != last )
    const char* next = find( first, last, ',' )
    vec.push_back( string( first, next - first ) )
    first = min( next + 1, last )

    copy( vec.begin(), vec.end()
    ostream_iterator<string>( cout
    "\n" ) )


    --
    Magic depends on tradition and belief. It does not welcom
    observation
    nor does it profit by experiment. On the other hand, science is base
    on experience; it is open to correction by observation and experiment
     
    Daniel T., Mar 30, 2006
    #6
    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. strtok problem

    , Aug 28, 2003, in forum: C Programming
    Replies:
    4
    Views:
    528
  2. Ram Laxman

    strtok problem

    Ram Laxman, Apr 11, 2004, in forum: C Programming
    Replies:
    3
    Views:
    5,121
    Ram Laxman
    May 3, 2004
  3. collinm

    strtok problem - strcmp

    collinm, Mar 24, 2005, in forum: C Programming
    Replies:
    4
    Views:
    825
    Mark McIntyre
    Mar 24, 2005
  4. Replies:
    3
    Views:
    524
    Steven Kobes
    Jul 27, 2005
  5. ern

    strtok() problem

    ern, Sep 20, 2005, in forum: C Programming
    Replies:
    12
    Views:
    650
    Default User
    Sep 22, 2005
Loading...

Share This Page