sscanf_强大的数据读取-转换
-
function
<cstdio>
sscanf
int sscanf ( const char * s, const char * format, ...);
Read formatted data from string
Reads data from s and stores them according to parameter format into the locations given by the additional arguments, as if scanf was used, but reading from s instead of the standard input (stdin). The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string. Parameters
s
C string that the function processes as its source to retrieve the data.
format
C string that contains a format string that follows the same specifications as format in scanf (see scanf for details).
... (additional arguments)
Depending on the format string, the function may expect a sequence of additional arguments, each containing a pointer to allocated storage where the interpretation of the extracted characters is stored with the appropriate type.
There should be at least as many of these arguments as the number of values stored by the format specifiers. Additional arguments are ignored by the function. Return Value
On success, the function returns the number of items in the argument list successfully filled. This count can match the expected number of items or be less (even zero) in the case of a matching failure.
In the case of an input failure before any data could be successfully interpreted, EOF is returned.
头文件
返回值
sscanf与scanf类似,都是用于输入的,只是后者以键盘(stdin)为输入源,前者以固定字符串为输入源。
格式控制符,类似于正则表达式:
支持集合操作
例子
|
1
2
3
|
char buf[512];sscanf("123456","%s",buf);//此处buf是数组名,它的意思是将123456以%s的形式存入buf中!printf("%s\n",buf); |
|
1
2
|
sscanf("123456","%4s",buf);printf("%s\n",buf); |
|
1
2
|
sscanf("123456abcdedf","%[^a-z]",buf);printf("%s\n",buf); |
|
1
2
|
sscanf("123456abcdedfBCDEF","%[1-9a-z]",buf);printf("%s\n",buf); |
|
1
|
printf("%s\n",buf); |
|
1
2
|
sscanf("123456abcdedfBCDEF","%[^A-Z]",buf);printf("%s\n",buf); |
|
1
2
|
sscanf("iios/12DDWDFF@122","%*[^/]/%[^@]",buf);printf("%s\n",buf); |
|
1
2
|
sscanf(“hello, world”,"%*s%s",buf);printf("%s\n",buf); |
|
1
2
|
sscanf(“字符串1\t字符串2\t字符串3”,"%s%s%s",str1,str2,str3);printf("%s\t%s\t%s\n",str1,str2,str3); |
sscanf及sscanf_s的常用方法,原来安全版本的函数,对参数和缓冲边界做了检查,增加了返回值和抛出异常。这样增加了函数的安全性,减少了出错的几率。
同时这也意味着在使用这些函数时,有时你不得不输入更多的关于缓冲区大小的参数,多敲几下键盘能换来更少的麻烦,值得!
下面总结了sscanf的以及sscanf_s的常用方法,也体现了“_s”版本函数与原函数的特别之处:
1、sscanf和scanf的不同是输入来源,前者是一个字符串,后者则是标准输入设备
2、sscanf的使用,以解析时间字符串为例,将字符串“2009-01-02_11:12:13”解析为整型年月日时分秒
//定义
char cc;
tm tm_temp={0};
string stime("2009-01-02_11:12:13");
(1) 必须严格按照分隔符形式匹配填写,若遇到不匹配项则终止解析
sscanf(stime.c_str(), "%4d-%2d-%2d_%2d:%2d:%2d",
&tm_temp.tm_year,
&tm_temp.tm_mon,
&tm_temp.tm_mday,
&tm_temp.tm_hour,
&tm_temp.tm_min,
&tm_temp.tm_sec
);
(2) 可以不按照分割符号形式填写,字符数必须一致,例如可以正确解析“2009/01/02_11:12:13”
sscanf(stime.c_str(), "%4d%c%2d%c%2d%c%2d%c%2d%c%2d",
&tm_temp.tm_year, &cc,
&tm_temp.tm_mon, &cc,
&tm_temp.tm_mday, &cc,
&tm_temp.tm_hour, &cc,
&tm_temp.tm_min, &cc,
&tm_temp.tm_sec
);
(3) 可以不按照分割符号形式填写,字符数必须一致,同上,%1s可以等同于%c
sscanf(stime.c_str(), "%4d%1s%2d%1s%2d%1s%2d%1s%2d%1s%2d",
&tm_temp.tm_year, &cc,
&tm_temp.tm_mon, &cc,
&tm_temp.tm_mday, &cc,
&tm_temp.tm_hour, &cc,
&tm_temp.tm_min, &cc,
&tm_temp.tm_sec
);
(4) 可以不按照分割符形式和数量填写,类型必须一致,例如可以正确解析“2009/01/02___11:12:13”
这里使用了sscanf的正则表达式,与通用的正则表示类似但不完全相同,%*c表示忽略连续多个字符
sscanf(stime.c_str(), "%4d%*c%2d%*c%2d%*c%2d%*c%2d%*c%2d",
&tm_temp.tm_year,
&tm_temp.tm_mon,
&tm_temp.tm_mday,
&tm_temp.tm_hour,
&tm_temp.tm_min,
&tm_temp.tm_sec
);
3、sscanf_s的使用
//定义
char cc[2];
tm tm_temp={0};
string stime("2009-01-02_11:12:13");
(1) 与sscanf第一种方法相同,可以使用"%4d-%2d-%2d_%2d:%2d:%2d"格式匹配解析
sscanf_s(stime.c_str(), "%4d-%2d-%2d_%2d:%2d:%2d",
&tm_temp.tm_year,
&tm_temp.tm_mon,
&tm_temp.tm_mday,
&tm_temp.tm_hour,
&tm_temp.tm_min,
&tm_temp.tm_sec
);
(2) 使用%c格式对数据解析时,必须对相应的缓冲区增加长度参数,否则将会出错
sscanf_s(stime.c_str(), "%4d%c%2d%c%2d%c%2d%c%2d%c%2d",
&tm_temp.tm_year, &cc, 1,
&tm_temp.tm_mon, &cc, 1,
&tm_temp.tm_mday, &cc, 1,
&tm_temp.tm_hour, &cc, 1,
&tm_temp.tm_min, &cc, 1,
&tm_temp.tm_sec
);
(3) 使用%s格式对数据解析时,缓冲长度必须大于字符串长度,否则不予解析
sscanf_s(stime.c_str(), "%4d%1s%2d%1s%2d%1s%2d%1s%2d%1s%2d",
&tm_temp.tm_year, &cc, 2,
&tm_temp.tm_mon, &cc, 2,
&tm_temp.tm_mday, &cc, 2,
&tm_temp.tm_hour, &cc, 2,
&tm_temp.tm_min, &cc, 2,
&tm_temp.tm_sec
);
(4) 与sscanf一样,sscanf_s同样支持正则表达式
sscanf_s(stime.c_str(), "%4d%*c%2d%*c%2d%*c%2d%*c%2d%*c%2d",
&tm_temp.tm_year,
&tm_temp.tm_mon,
&tm_temp.tm_mday,
&tm_temp.tm_hour,
&tm_temp.tm_min,
&tm_temp.tm_sec
);
通过以上对比sscanf与sscanf_s的使用,可以看出后者对缓冲区安全有了更多的考虑,从而避免了许多不经意的烦恼。
大家都知道sscanf是一个很好用的函数,利用它可以从字符串中取出整数、浮点数和字符串等等。它的使用方法简单,特别对于整数和浮点数来说。但新手可 能并不知道处理字符串时的一些高级用法,这里做个简要说明吧。
string timeIn = "2016-09-17 09:48:13.560";
tm tm_timeIn = { };
int timeOutMsecTemp = ;
sscanf_s(timeIn.c_str(), "%4d-%2d-%2d %2d:%2d:%2d.%3d",
&tm_timeIn.tm_year,
&tm_timeIn.tm_mon,
&tm_timeIn.tm_mday,
&tm_timeIn.tm_hour,
&tm_timeIn.tm_min,
&tm_timeIn.tm_sec,
&timeInMsecTemp);
tm_timeIn.tm_year -= ; //年份,指的是与1900年的差值
tm_timeIn.tm_mon -= ; //月份,0代表1月,故应减去1
char dt[];
ctime_s(dt, sizeof dt,&time_t_In);
cout << dt << endl; //Sat Sep 17 09:48:13 2016\n
char bat[];
asctime_s(bat, sizeof bat, &tm_timeIn);
cout << bat << endl;
sscanf_强大的数据读取-转换的更多相关文章
- MATLAB对于文本文件(txt)数据读取的技巧总结(经典中的经典)
振动论坛原版主eight的经典贴http://www.chinavib.com/thread-45622-1-1.html MATLAB对于文本文件(txt)进行数据读取的技巧总结(经典中的经典)由于 ...
- 利用泛型和反射,管理配置文件,把Model转换成数据行,并把数据行转换成Model
利用泛型和反射,管理配置文件,把Model转换成数据行,并把数据行转换成Model 使用场景:网站配置项目,为了便于管理,网站有几个Model类来管理配置文件, 比如ConfigWebsiteMo ...
- OpenCV中IplImage图像格式与BYTE图像数据的转换
最近在将Karlsruhe Institute of Technology的Andreas Geiger发表在ACCV2010上的Efficent Large-Scale Stereo Matchin ...
- TableInputFormat分片及分片数据读取源码级分析
我们在MapReduce中TextInputFormat分片和读取分片数据源码级分析 这篇中以TextInputFormat为例讲解了InputFormat的分片过程以及RecordReader读取分 ...
- Java学习-028-JSON 之二 -- 数据读取
JSON数据由 JSONObject.JSONArray.key_value 组合而成.通常来说,JSONObject 可以包含 JSONObject.JSONArray.key_value:JSON ...
- [SAP ABAP开发技术总结]数据输入输出转换、小数位/单位/货币格式化
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- 数据表转换成json(DatatableToJson)
#region 转换Table为JSON数据 /// <summary> /// 转换Table为JSON数据 /// </summary> /// <param nam ...
- TensorFlow实践笔记(一):数据读取
本文整理了TensorFlow中的数据读取方法,在TensorFlow中主要有三种方法读取数据: Feeding:由Python提供数据. Preloaded data:预加载数据. Reading ...
- 【强大的PDF格式转换工具】Lighten PDF Converter OCR for Mac 6.2.0
[简介] Lighten PDF Converter OCR 是一款Mac上强大的PDF格式转换工具,可以将PDF文档快速批量的转换为Office (Word, Excel, PowerPoint), ...
随机推荐
- Django-Admin后台管理
Rhel6.5 Django1.10 Python3.5 应用环境:Python+Virtualenv(Python Virtualenv运行Django环境配置) Django-Admin后台管理 ...
- MVC -- 后台RedirectToAction传递实体类与字符串
1.MVC -- 后台RedirectToAction传递实体类 RedirectToAction(控制器,控制器方法,实体类) 2.MVC -- 后台RedirectToAction传递字符串 Re ...
- 良心版Dolby Home Theater v4.1安装教程
感(pi)谢(pan)一下两个教程: 文库文章链接:http://wenku.baidu.com/link?url=beBg_apvCuY3xiCXk4zl65Q7AmeCjoDGMol03K0xhk ...
- AngularJS 动画
AngularJS 提供了动画效果,可以配合 CSS 使用. AngularJS 使用动画需要引入 angular-animate.min.js 库. <script src="htt ...
- log4net配置
1.configuration配置 <configSections> <section name="log4net" type="log4net.Con ...
- 【Java EE 学习 72 下】【数据采集系统第四天】【移动/复制页分析】【使用串行化技术实现深度复制】
一.移动.复制页的逻辑实现 移动.复制页的功能是在设计调查页面的时候需要实现的功能.规则是如果在同一个调查中的话就是移动,如果是在不同调查中的就是复制. 无论是移动还是复制,都需要注意一个问题,那就是 ...
- Oracle并发与多版本控制
1.什么是并发 2.事务隔离级别 2.1 READ UNCOMMITTED 2.2 READ COMMITTED 2.3 REPETABLE READ 2.4 SERIALIZ ...
- 如何把Qlik Sense嵌入到Web应用中
(此文章同时发表在本人微信公众号"dotNET开发经验谈",欢迎右边二维码来关注.) 题记:这是一个给初学者(尤其对VS不熟悉的BI工程师)的入门操作向导. Qlik Sense是 ...
- 2016ACM/ICPC亚洲区大连站-重现赛(感谢大连海事大学)(7/10)
1001题意:n个人,给m对敌对关系,X个好人,Y个坏人.现在问你是否每个人都是要么是好人,要么是坏人. 先看看与X,Y个人有联通的人是否有矛盾,没有矛盾的话咋就继续遍历那些不确定的人关系,随便取一个 ...
- CSS 代码技巧与维护 ★ Mozilla Hacks – the Web developer blog
原文链接:https://hacks.mozilla.org/2016/05/css-coding-techniques/ 译文链接 :http://www.zcfy.cc/article/css-c ...