I have a little bit of a problem while understanding the sscanf string formatting.
I have that string stored in str: 192.168.0.100/act?bla=
I want with this code to have bla stored inside my “key” variable and the remaining stuff (after the ‘=’) in my “buf” variable
char str[] = "192.168.0.100/act?bla=";
char key[20];
char buf[100];
sscanf(str, "%*[^?] %[^=] %s", key, buf);
The
?and=will not be consumed so include them in the format specifier:See demo at http://ideone.com/YoRMh3 .
To prevent buffer overrun specify the maximum number of characters that can be read by each specifier, one less than the target array to allow for null termination, and ensure that both arrays were populated by checking the return value of
sscanf():In order to ensure that the
bufvalue is not truncated, you can use the%nformat specifier that populates anintindicating the position at which processing stopped (note the%nhas no effect on the return value ofsscanf()). If the entire input was processed the end position isstrlen(str):