|
-
Nov 12th, 2002, 08:27 AM
#1
Thread Starter
Addicted Member
strtok problems
Hello
I am having problems using strtok on string like:
1,,Andrew,Dunn,215,63,,,342,,,,,
each comma delimits a record, so i need the string to tokenise to:
record-1
record-
record-Andrew
record-Dunn
record-215
record-63
record-
record-
record-342.....etc
But strtok seems to ignore the records that are ,,.
Is there an easy way around this?
cheers
in advance
Andy
-
Nov 12th, 2002, 10:51 AM
#2
Monday Morning Lunatic
Are you using C or C++? If C++, I'd suggest boost::tokenizer instead.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 12th, 2002, 10:54 AM
#3
Frenzied Member
in vanilla C:
Code:
#include <string.h>
#include <stdio.h>
int main(){
char working[80];
char *buf,*end;
char comma = ',';
int flds=0;
strcpy(working,"1,,Andrew,Dunn,215,63,,,342,,,,,");
for(end=working;*end;end++) if(*end==comma) *end=0x00;
for(buf=working;buf<end;){
flds++;
printf("field: %d:%s\n",flds,buf);
for(;*buf;buf++);
buf++;
}
return 0;
}
-
Nov 12th, 2002, 11:14 AM
#4
Thread Starter
Addicted Member
excellent
Cheers boys
parksie, thanks i've been looking at the boost tokenise
and thanks jim, your suggestion works as well, just have to see which one i'm allowed to use!
Thanks again
Andy
-
Nov 12th, 2002, 11:29 AM
#5
Monday Morning Lunatic
Jim's will almost certainly be the fastest method, but he's dropped out a large proportion of the error checking from that.
PS: If your teachers or whatever complain about using anything from Boost, point them at the "who's who" and they'll see a surprising correlation with the standards committee
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|