sscanf()与sprintf()

sscanf()可以理解为:string+scanf;

ssprintf()可以理解为:string+printf;

两者均在#include<cstdio>头文件下

 

理解:

scanf("%d",&n);

printf("%d",n);

可以理解为:(screen表示屏幕)

scanf(screen,"%d",&n);把screen的内容以"%d"的格式传输到n中(从左到右)

printf(screen,"%d",n);把n以"%d"的格式传输到screen上(从右到左)

 

那么理解sscanf和ssprintf就非常好理解了,把screen换成了字符串而已

sscanf(str,"%d",&n);

sprintf(str,"%d",n);

 

1.sscanf的简单例子:

#include<cstdio>
using namespace std;
int main()
{
	int n;
	char str[100] = "123";
	sscanf(str, "%d", &n);
	printf("%d\n", n);
	return 0;
}

输出结果:123

2.sprintf的简单例子:

#include<cstdio>
using namespace std;
int main()
{
	int n=233;
	char str[100] ;
	sprintf(str, "%d", n);
	printf("%s",str);
	return 0;
}

输出结果:

233

 

3.sscanf比较复杂的应用

#include<cstdio>
using namespace std;
int main()
{
	int n;
	double db;
	char str[] = "2018:3.14,hello";
	char s[100];
	sscanf(str, "%d:%lf,%s", &n, &db, s);
	printf("n=%d  db=%.2f  s=%s", n, db, s);
	return 0;
}

输出结果:
n=2018  db=3.14  s=hello

 

4.sprintf的比较复杂写法:

#include<cstdio>
using namespace std;
int main()
{
	int n=12;
	double db=3.1415;
	char s[] = "good";
	char str[100];
	sprintf(str, "%d:%lf,%s", n, db, s);
	printf("%s\n",str);
	return 0;
}

输出结果:

12:3.141500,good

 

 

 

5.sscanf还支持正则运算,许多问题配合着正则运算都可以解决:

正则表达式

  • 15
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值