sscanf与sprintf函数的使用实例(C / C++)
来源:互联网 发布:酒店数据接口 编辑:程序博客网 时间:2024/05/17 01:30
函数介绍
sscanf() - 从一个字符串中读进与指定格式相符的数据。
我们平时使用的 scanf()是从键盘读进与指定格式相符的数据。
函数原型
int sscanf ( const char * , const char * , ... );
int sscanf ( const char * buffer , const char * format , [argument ]... );
buffer : 存储的数据
format : 格式控制字符串
argument : 选择性设定字符串
sscanf 从buffer 读进数据,依照 format 的格式将数据写入 argument 。
其中,“%s”可做很多微妙的修改,比如:
“%3s”,那么就取前面三个字符(result : lgh);
"%[^ ]",读到空格为止;
"%[1-9a-z]":取仅包含数字和小写字母的字符;
函数使用实例
1. 字符串读入字符串
char buf[1314];sscanf("lghaywn","%s",buf);printf("%s\n",buf);result : lghaywn
其中,“%s”可做很多微妙的修改,比如:
“%3s”,那么就取前面三个字符(result : lgh);
"%[^ ]",读到空格为止;
"%[1-9a-z]":取仅包含数字和小写字母的字符;
2.字符串分割读取
int year,month,day;sscanf("2015:9:7","%d:%d:%d",&year,&month,&day);printf("%04d:%02d:%02d\n",year,month,day);result : 2015:09:07
3.字符串与数字之间的互换
string number = "-2015.9";double numberDate;sscanf(number.c_str(),"%lf",&numberDate);printf("%.1lf\n",numberDate);result : -2015.9
double numberDate2 = -2015.3;char number2[10];sprintf(number2,"%lf",numberDate2);cout<<number2<<endl;result : -2015.300000
0 0
- sscanf与sprintf函数的使用实例(C / C++)
- C语言sprintf与sscanf函数总结及fscanf,sscanf,fprintf,sprintf之间的区别
- C语言sprintf与sscanf函数[总结]
- C语言sprintf与sscanf函数[总结]
- C语言sprintf与sscanf函数[总结]
- C语言sprintf与sscanf函数[总结]
- C语言sprintf与sscanf函数[总结]
- C语言sprintf与sscanf函数
- C语言sprintf与sscanf函数[总结]
- C语言sprintf与sscanf函数[总结]
- C语言sprintf与sscanf函数[总结]
- C语言sprintf与sscanf函数
- C语言sprintf与sscanf函数[总结]
- C语言 sscanf,sprintf函数 使用总结
- c函数之sprintf,sscanf函数使用(字符串与整型互转函数)
- C语言 sscanf 和 sprintf函数的说明和使用
- C语言sscanf和sprintf的用法及实例
- sprintf(),sscanf()函数的使用
- C语言指针 int *p[10] int (*p)[10] int *p(int) int (*p)(int) 区别与举例
- TestNG中的参数Parameters概念
- Java批量更改文件名
- hdu3275 线段树区间更新(略坑
- 并查集刷题
- sscanf与sprintf函数的使用实例(C / C++)
- shell基础
- 内核启动信息Nandflash分区中出现ftl_cs: FTL header not found
- 网络流专题
- 自学java(5)
- Android SDK Manager安装Platform-tools
- 微信JSSDK获取当前地理位置信息
- 机房收费之附加数据库
- Lab2:Distribute Machine Learning: Computer and Storage