Double split in C

Posted by Dmitri on Stack Overflow See other posts from Stack Overflow or by Dmitri
Published on 2010-04-10T09:39:13Z Indexed on 2010/04/10 9:43 UTC
Read the original article Hit count: 541

Filed under:
|
|
|

Hi again dear community!

OK. For example I have this line in my txt file:

1|1,12;7,19;6,4;8,19;2,2 . As you can see, it has 2 parts, separated by "|". I have no problems getting both parts, and separating second part ( 1,12;7,19;6,4;8,19;2,2 ) using ";" separator. BUT I do have problems with separating further by "," to get first and second number of each set.

This is my current code:

         result = strtok(result, ";");

         while(result != NULL ) {
            printf("%s\n", result);

            result = strtok(NULL, ";");
         }

It outputs me: 1,12 7,19 6,4 8,19 2,2

OK, great. But when I try to 'strtok' (I'm using this method for splitting) like this:

        result = strtok(result, ";");

         while(result != NULL ) {
            //printf("%s\n", result);
            help    = strtok(result, ",");    
            while(help != NULL) {
               printf("<%s>", help);
               help = strtok(NULL, ",");
            }

            result  = strtok(NULL, ";");
         }

I only get "<1>,<12>" like there is only one set in this set of numbers. I dont understand where are the rest of the numbers. Instead, output should be: <1>,<12>,<7>,<19>,<6>,<4>,<8>,<19>,<2>,<2>. Could someone please give a solution, how to get EACH number of each set this set of numbers. Maybe there are other methods or I'm doing something wrong :)

Thank you! Best Regards, Dmitri

© Stack Overflow or respective owner

Related posts about c

    Related posts about split