#include<stdio.h>

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是一个很好用的函数,利用它可以从字符串中取出整数、浮点数和字符串等等。它的使用方法简单,特别对于整数和浮点数来说。但新手可
能并不知道处理字符串时的一些高级用法,这里做个简要说明吧。

  1. 常见用法。

以下是引用片段:
  char str[512] = ;
  sscanf("123456 ", "%s", str);
  printf("str=%sn", str);

  2. 取指定长度的字符串。如在下例中,取最大长度为4字节的字符串。

以下是引用片段:
  sscanf("123456 ", "%4s", str);
  printf("str=%sn", str);

  3. 取到指定字符为止的字符串。如在下例中,取遇到空格为止字符串。

以下是引用片段:
  sscanf("123456 abcdedf", "%[^ ]", str);
  printf("str=%sn", str);

  4. 取仅包含指定字符集的字符串。如在下例中,取仅包含1到9和小写字母的字符串。

以下是引用片段:
  sscanf("123456abcdedfBCDEF", "%[1-9a-z]", str);
  printf("str=%sn", str);

  5. 取到指定字符集为止的字符串。如在下例中,取遇到大写字母为止的字符串。

以下是引用片段:
  sscanf("sscanf("123456abcdedfBCDEF", "%[^A-Z]", str);
  printf("str=%sn", str);", str);
  printf("str=%sn", str);
 

sscanf的更多相关文章

  1. C语言关于利用sscanf实现字符串相加减

    #include<stdio.h>#include<string.h>void main(){ int a; int b; char str1[10] = "9999 ...

  2. sscanf提取字符串中的数据php

    1.需求 理解sscanf的作用 2.例子 $str = "age:30 weight:60kg"; sscanf($str,"age:%d weight:%dkg&qu ...

  3. sscanf与正则表达式(转)

    今天翻google reader的时候看到这样一篇文章,介绍的是sscanf的高级用法.直到今天我才知道sscanf是可以直接用正则表达式的,惭愧. 在msdn中sscanf的声明如下 int ssc ...

  4. C语言函数sscanf()的用法

    从文件读取数据是一件很麻烦的事,所幸有sscanf()函数. C语言函数sscanf()的用法 sscanf() - 从一个字符串中读进与指定格式相符的数据. 函数原型: int sscanf( st ...

  5. sscanf()函数的使用及其实例

    资料引自: 传送门 sscanf函数原型: Int sscanf( const char * src, const char * format, ...); int scanf( const char ...

  6. C 语言sscanf

    C语言以sscanf逗号作为分割符 ]={}; ]={}; ]={}; sscanf(],&buf_b[],&buf_b[]); printf("************** ...

  7. Babelfish(二分查找,字符串的处理略有难度,用sscanf输入)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 28581   Accepted: 12326 题目链接: ...

  8. (转)sscanf() - 从一个字符串中读进与指定格式相符的数据

    (转)sscanf() - 从一个字符串中读进与指定格式相符的数据 sscanf() - 从一个字符串中读进与指定格式相符的数据. 函数原型: Int sscanf( string str, stri ...

  9. sscanf函数

    sscanf函数用法举例 #include <stdio.h> #include <string.h> #define N 512 int main() { char buf[ ...

  10. hdu 1036 (I/O routines, fgets, sscanf, %02d, rounding, atoi, strtol) 分类: hdoj 2015-06-16 19:37 32人阅读 评论(0) 收藏

    thanks to http://stackoverflow.com/questions/2144459/using-scanf-to-accept-user-input and http://sta ...

随机推荐

  1. GraphicsLab Project之辉光(Glare,Glow)效果 【转】

    作者:i_dovelemon 日期:2016 / 07 / 02 来源:CSDN 主题:Render to Texture, Post process, Glare, Glow, Multi-pass ...

  2. 【Git】Git hangs while unpacking objects (Windows)

    Git hangs while unpacking objects (Windows) 14 Oct 2014 I'm not sure if this is because we're behind ...

  3. Shell实现循环执行curl向Solr导入json文件

    #!/bin/bash for file in ./文件夹名/* do echo $file curl "http://IP:8983/solr/集合名/update?commit=true ...

  4. 【千纸诗书】—— PHP/MySQL二手书网站后台开发之项目设计

    前言:这个项目是毕设时候做的,我负责后台数据操作部分,已经很久了,这次回顾这部分,是为了复习PHP和MySQL的知识,正好现在在公司也负责的是后台管理系统的业务.第一篇[项目概况]附上毕业论文部分节选 ...

  5. 本地搭建ELK系统

    ELK系统主要由三部分组成,各自是elasticsearch.logstash.kibana. ELK系统收到推送过来的日志后.首先由logstash解析日志中的字段,分解成一个一个的关键字. ela ...

  6. 别样JAVA学习(六)继承下(2.3)异常下

    1.RuntimeException Exception中有一个特殊的子类异常RuntimeException执行时异常. 假设在函数内容抛出该异常,函数上能够不用声明.编译一样通过. 假设在函数上声 ...

  7. redis源代码分析(5)——aof

    前面几篇基本介绍了redis的主要功能.流程.接下来是一些相对独立的部分,首先看一下持久化. redis持久化支持两种方式:RDB和AOF,我们首先看一下AOF的实现. AOF(Append only ...

  8. 【VBA编程】15.WorkSheet对象常用属性

    [Cells属性] Cells属性用于返回一个Range对象,表示工作表中所有的单元格,包括已经使用的单元格和未使用的单元格. [代码区域] Sub 使用Cells属性() '声明变量 Dim wk ...

  9. 豆瓣电台笔记3:cell添加从中间向两侧放大的动画

    步骤: 1.设置动画属性的初始值 cell.layer.transform = CATransform3DMakeScale(0.1,0.1,1) 2.在指定时间内执行动画 UIView.animat ...

  10. 使用 Google 高级搜索的一些技巧

      一,GOOGLE简介 Google(www.google.com)是一个搜索引擎,由两个斯坦福大学博士生Larry Page与Sergey Brin于1998年9月发明,Google Inc. 于 ...