int sprintf ( char * str, const char * format, ... );
str - Pointer to a buffer where the resulting C-string is stored (should be large enough)
format - same as format in printf
... - additional argumentsreturn: strlen(str) - the total number of characters written
char buffer [50]; int id = 1234567; int n = sprintf (buffer, "My ID is %d", id); // buffer: My ID is 1234567 // n: 16
int sscanf ( const char * s, const char * format, ...);
s - C string that the function processes as its source to retrieve the data.
format - same as format in scanf
... - additional argumentsreturn: the number of items in the argument list successfully filled.
char buffer [] = "Tom 18,7817"; char name[20]; int age, id; sscanf(buffer, "%s %d,%d",name,&age,&id); // s: Tom age: 18 id: 7817
PS. sscanf虽然很强大,但是难以处理带空格的(或者空格不在开头结尾的)字符串。
来源:oschina
链接:https://my.oschina.net/u/4342750/blog/4462454