Is there any function in C or C++ to perform the inverse of an snprintf, such that
char buffer[256]
snprintf( buffer, 256, "Number:%i", 10);
// Buffer now contains "Number:10"
int i;
inverse_snprintf(buffer,"Number:%i", &i);
// i now contains 10
I can write a function that meets this requirement myself, but is there already one within the standard libraries?
Yes, there is sscanf()
. It returns the number of tokens successfully matched to the input, so you can check the return value to see how far it made it into the input string.
if (sscanf(buffer, "Number:%i", &i) == 1) {
/* The number was successfully parsed */
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With