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

A

Adam Balgach

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.
 
R

Ron Natalie

Adam said:
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.
 
N

news-east

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.
 

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top