Problems with strtok() returning one too many tokens...

Discussion in 'C++' started by Adam Balgach, Nov 24, 2004.

  1. Adam Balgach

    Adam Balgach Guest

    Hello hello,

    i am trying to parse up a line that is in the format:

    @xxxxx yyyyyy {zzzzz, aaaaaa, bbbbbb}

    where there could be any number of elements inbetween the { ... }

    i am only concerned with getting the yyyyy and then the tokens in the
    { }

    so the code im using is:
    in->getline(line, 1024); //to get the inital line.
    char *name;
    name = strtok(line, " ");
    int count=0;
    char *firstThing;
    while(name!=NULL) {
    name = strtok(NULL, " {,}";
    if (count==0) {
    //do somehting with yyyyyy
    cout << "First Thing: "<<name<<endl;
    firstThing=name;
    }
    else if (count>0) {
    cout << "{"<<firstThing<<", "<<name<<"}\n";
    //do something with name (should = zzzzzzz then aaaaaaa hten
    bbbbbbb)
    }
    count++;
    }

    now when i run this code it outputs:
    First Thing: yyyyyy
    {yyyyyy,zzzzzz}
    {yyyyyy,aaaaaa}
    {yyyyyy,bbbbb}
    {yyyyyy,


    any idea why its running through this loop one too many times... ie
    the last entry shouldnt be htere?
    cheers,
    Adam.
     
    Adam Balgach, Nov 24, 2004
    #1
    1. Advertising

  2. Adam Balgach

    Ron Natalie Guest

    Adam Balgach wrote:
    > Hello hello,
    >
    > i am trying to parse up a line that is in the format:
    >

    Strtok is an abomination.


    > name = strtok(line, " ");
    > int count=0;
    > char *firstThing;
    > while(name!=NULL) {
    > name = strtok(NULL, " {,}";


    name may here return NULL but you don't test for it.
    (I assume you actually have the missing ) on the line above as well.
    > if (count==0) {
    > //do somehting with yyyyyy
    > cout << "First Thing: "<<name<<endl;


    UNDEFINED behavior happens when name == NULL.
     
    Ron Natalie, Nov 24, 2004
    #2
    1. Advertising

  3. Adam Balgach

    news-east Guest

    Definitely a case for reading up on find_first_of, find_last_not_of .. and
    the other 'find's; if you know you can go straight to yyyy from the first
    occurence of '{' and use the same marker to begin parsing your 'zzzzz',
    aaaaa, etc.'s, then just work out the most efficient way of doing so using
    the STL find functions.

    If you know the order of these strings, then an istream tokenizer would be a
    good bet.


    "Adam Balgach" <> wrote in message
    news:...
    > Hello hello,
    >
    > i am trying to parse up a line that is in the format:
    >
    > @xxxxx yyyyyy {zzzzz, aaaaaa, bbbbbb}
    >
    > where there could be any number of elements inbetween the { ... }
    >
    > i am only concerned with getting the yyyyy and then the tokens in the
    > { }
    >
    > so the code im using is:
    > in->getline(line, 1024); //to get the inital line.
    > char *name;
    > name = strtok(line, " ");
    > int count=0;
    > char *firstThing;
    > while(name!=NULL) {
    > name = strtok(NULL, " {,}";
    > if (count==0) {
    > //do somehting with yyyyyy
    > cout << "First Thing: "<<name<<endl;
    > firstThing=name;
    > }
    > else if (count>0) {
    > cout << "{"<<firstThing<<", "<<name<<"}\n";
    > //do something with name (should = zzzzzzz then aaaaaaa hten
    > bbbbbbb)
    > }
    > count++;
    > }
    >
    > now when i run this code it outputs:
    > First Thing: yyyyyy
    > {yyyyyy,zzzzzz}
    > {yyyyyy,aaaaaa}
    > {yyyyyy,bbbbb}
    > {yyyyyy,
    >
    >
    > any idea why its running through this loop one too many times... ie
    > the last entry shouldnt be htere?
    > cheers,
    > Adam.




    Posted Via Usenet.com Premium Usenet Newsgroup Services
    ----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    ----------------------------------------------------------
    http://www.usenet.com
     
    news-east, Nov 28, 2004
    #3
    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. Henrik
    Replies:
    1
    Views:
    550
    Phillip Ian
    Jun 2, 2005
  2. Replies:
    11
    Views:
    5,051
    Mark Bluemel
    Mar 13, 2008
  3. Dag Sunde
    Replies:
    4
    Views:
    358
  4. Evolution
    Replies:
    1
    Views:
    109
    Evolution
    Apr 15, 2011
  5. DFS
    Replies:
    92
    Views:
    253
    BartC
    Jun 17, 2014
Loading...

Share This Page