-
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
-
Are you using C or C++? If C++, I'd suggest boost::tokenizer instead.
-
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;
}
-
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
-
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 :D