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: 7817PS. sscanf虽然很强大,但是难以处理带空格的(或者空格不在开头结尾的)字符串。
本文介绍了C++中的sscanf和sprintf函数,包括它们的定义、参数和返回值。sprintf用于将格式化的数据写入字符串,而sscanf则从字符串中读取格式化的数据。尽管sscanf功能强大,但在处理包含空格的字符串时可能会遇到挑战。


被折叠的 条评论
为什么被折叠?



