Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problems with strtok() returning one too many tokens...

Reply
Thread Tools

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

 
 
Adam Balgach
Guest
Posts: n/a
 
      11-24-2004
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.
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      11-24-2004
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.
 
Reply With Quote
 
 
 
 
news-east
Guest
Posts: n/a
 
      11-28-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How many PHP calls is too many for one webpage? Evolution Javascript 1 04-15-2011 07:07 PM
Spring/Hibernate loadAll returning the same identity too many times... Daniel Pitts Java 0 10-18-2007 06:41 PM
LDAP search returning too many results John Gordon Java 5 12-08-2006 09:10 PM
Pagination function for search page returning too many hits to display on one page. Henrik ASP .Net 1 06-02-2005 01:17 PM
Too many (small) vs. too large linked script files in a document... Dag Sunde Javascript 4 12-16-2004 11:38 PM



Advertisments