Results 1 to 5 of 5

Thread: strtok problems

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Location
    UK
    Posts
    222

    Question 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

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  3. #3
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    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;
    }

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Location
    UK
    Posts
    222

    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

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
  •  



Click Here to Expand Forum to Full Width