Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 227 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Double split in C

#1
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!
Reply

#2
The first strtok splits the original into several null terminated strings. The second strtok is only applied to the first of these strings - it stops at the first null-terminator created by the first strtok call. You need to call the second strtok code for EACH of the strings created by the first.
Reply

#3
In addition to what Neil pointed out regarding modifying the original string, the `strtok()` function is not designed to be used in a nested manner like you describe. You may wish to investigate the `strtok_r()` function, or avoid the use of the `strtok*` family altogether.
Reply

#4
C's `strtok` is pretty evil in that it modifies the source string, inserting NULL in place of your separator. Thus your second loop stops when it reaches the NULL inserted by the outer loop. What you want is to save the pointers to your `;`-separated strings somewhere else, and then `strtok` each of them separately.

Or, if you do want to use nested loops, [see here](

[To see links please register here]

).
Reply

#5
>**char *strtok(char *str, const char *delim);** (from man pages)
The _delim_ argument specifies a set of characters that delimit the tokens in the parsed string. The caller may specify different strings in _delim_ in successive calls that parse the same string.

So, use both `;` and `,` as delimiter to get all the numbers.

//this will delimit result whenever ";" or "," is found
result = strtok(result, ";,");

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

Reply

#6
As others have pointed out, `strtok()` isn't reentrant - it maintains internal state about the string it's parsing, so it can't be used to parse two different strings simultaneously (which means two different non-NULL values for its first argument, so your situation counts).

If you have the reentrant version `strtok_r()` available, you can change your code to use it like so:

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);
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through