Reputation: 641
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?
Upvotes: 5
Views: 4111
Reputation: 35
In addition to sscanf()
, if you are only going to scan numbers, you can safely scan numeric values using the strto*()
functions: strtol()
, strtoll()
, strtoul()
, strtoull()
, strtof()
, strtod()
, strtold()
. You can also use atoi()
, atol()
and atoll()
, but note that these functions return 0
if the string is not an integer that can be converted.
Upvotes: 0