sprintf
#include<cstdio>
using namespace std;
int main()
{
int n = 2048;
double db = 3.14;
char str[100] = "hello", str2[100];
sprintf(str2,"%d, %lf, %s",n,db,str );
printf(str2);
}
输出结果
2048, 3.140000, hello
sprintf就是将n, db, str(右边参数)以某种格式(“%d %lf %s”)传入str2(左边参数)中
右边参数可以有多个
sscanf
#include<cstdio>
using namespace std;
int main()
{
int n;
double db;
char str[100] = "2048:3.14, hello", str2[100];
sscanf(str, "%d:%lf,%s", &n, &db, str2);
printf("%d:%lf,%s", n, db, str2);
}
输出结果
2048:3.140000,hello
sscanf就是将参数str(左侧),以某种形式(“%d %lf %s”)传给n, db, str2(右边参数)各取所需