Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inverse of snprintf

Tags:

c++

c

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?

like image 550
foips Avatar asked Dec 15 '14 15:12

foips


1 Answers

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 */
}
like image 91
cdhowie Avatar answered Sep 23 '22 14:09

cdhowie