Double division by C

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 consists of two parts, separated by the symbol | . I have no problem getting both parts and splitting the second part 1,12;7,19;6,4;8,19;2,2 using a separator ; . BUT I am having problems with further separation, to get the 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, ";"); } 

He leads me out:

1.12
7.19
6.4
8.19
2.2

Good Excellent. But when I try "strtok" (I use this method for separation) for example:

  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 get only "<1>, <12>", as if there is only one set in this set of numbers. I don’t understand where the rest of the numbers are. Instead, the output should be: <1 <12> <7> <19> <6 <4>, <8 <19 <, <2>. Can someone please give a decision how to get EVERY number from each set of this set of numbers. Maybe there are other methods, or I'm doing something wrong :)

Thanks!

+6
c string split strtok
source share
5 answers

char * strtok (char * str, const char * delim); (from man pages)
The delim argument specifies the character set that restricts the tokens in the parsed string. The caller may indicate different lines in delim in consecutive calls that parse the same line.

So use how ; , and as a separator to get all numbers.

 //this will delimit result whenever ";" or "," is found result = strtok(result, ";,"); while(result != NULL ) { printf("%s\n", result); result = strtok(NULL, ";,"); } 
+2
source share

In addition to what Neil pointed out about modifying the original string, the strtok() function strtok() not intended to be used in a nested manner, as you describe. You can learn the strtok_r() function or not use the strtok* family at all.

+5
source share

The first strtok splits the original into multiple lines with zero completion. The second strtok applies only to the first of these lines - it stops at the first null terminator created by the first call to strtok. You need to call the second strtok code for EACH of the lines created by the first.

+1
source share

C strtok pretty wicked as it modifies the original string by inserting NULL instead of your delimiter. So your second loop stops when it reaches NULL inserted by the outer loop. You want to keep pointers to your own ; -separated strings somewhere else, and then strtok each one separately.

Or, if you want to use nested loops, see here .

0
source share

As others noted, strtok() not reentrant - it maintains the internal state of the string that it parses, so it cannot be used to parse two different strings at the same time (which means two different non-NULL values ​​for its first argument, so your situation has the meaning).

If you have a version of strtok_r() , you can change your code to use it like this:

 char *st_result, *st_help; result = strtok_r(result, ";", &st_result); while (result) { printf("[%s]", result); help = strtok_r(result, ",", &st_help); while (help) { printf("<%s>", help); help = strtok_r(NULL, ",", &st_help); } result = strtok_r(NULL, ";", &st_result); } 
0
source share

All Articles