473,378 Members | 1,280 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

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.
Jul 22 '05 #1
2 2045
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.
Jul 22 '05 #2
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" <ff**@hotmail.com> wrote in message
news:a5**************************@posting.google.c 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
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: ern | last post by:
I'm using strtok( ) to capture lines of input. After I call "splitCommand", I call strtok( ) again to get the next line. Strtok( ) returns NULL (but there is more in the file...). That didn't...
8
by: manochavishal | last post by:
Hi I am writing a Program in which i get input as #C1012,S,A#C1013,S,U I want to get C1012,S,A using strtok and then pass this to function CreateVideo which will further strtok this...
6
by: plmanikandan | last post by:
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 ...
33
by: Geometer | last post by:
Hello, and good whatever daytime is at your place.. please can somebody tell me, what the standard behavior of strtok shall be, if it encounters two or more consecutive delimiters like in...
18
by: Robbie Hatley | last post by:
A couple of days ago I dedecided to force myself to really learn exactly what "strtok" does, and how to use it. I figured I'd just look it up in some book and that would be that. I figured...
4
by: Michael | last post by:
Hi, I have a proble I don't understand when using strtok(). It seems that if I make a call to strtok(), then make a call to another function that also makes use of strtok(), the original call is...
26
by: ryampolsky | last post by:
I'm using strtok to break apart a colon-delimited string. It basically works, but it looks like strtok skips over empty sections. In other words, if the string has 2 colons in a row, it doesn't...
14
by: Mr John FO Evans | last post by:
I cam across an interesting limitation to the use of strtok. I have two strings on which I want strtok to operate. However since strtok has only one memory of the residual string I must complete...
75
by: siddhu | last post by:
Dear experts, As I know strtok_r is re-entrant version of strtok. strtok_r() is called with s1(lets say) as its first parameter. Remaining tokens from s1 are obtained by calling strtok_r() with...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.