#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. VR开发者必看:4大最为值得关注的内容平台【转】

            时间 2016-01-19 14:12:57 原文  http://www.sfw.cn/xinwen/478369.html 主题 虚拟现实 Oculus 对很多有意涉及VR行业的内 ...

  2. Snapdragon profiler

    这个debugger似乎看不了constant buffer 看不了memory but有个很神奇的功能 改shader直接在手机上显示结果 注意 需要unity build的时候勾 Script D ...

  3. 【千纸诗书】—— PHP/MySQL二手书网站后台开发之知识点记录

    前言:使用PHP和MySQL开发后台管理系统的过程中,发现有一些通用的[套路小Tip],这里集中记录一下.结合工作中ing的后台业务,我逐渐体会到:除了技术知识外.能使用户体验好的“使用流程设计”积累 ...

  4. Java8 更快的原子类:LongAdder(笔记)

    更快的原子类:LongAdder      大家对AtomicInteger的基本实现机制应该比较了解,它们是在一个死循环内,不断尝试修改目标值,知道修改成功,如果竞争不激烈,那么修改成功的概率就很高 ...

  5. python 使用 urllib2

    使用basic auth 的3种方式 1. 设置header import urllib2 from base64 import encodestring headers = {'Content-Ty ...

  6. TP视图命名规则之一

    TP视图命名规则之一   如果觉得目录结构太深,可以通过设置 TMPL_FILE_DEPR 参数来配置简化模板的目录层次,例如设置: 'TMPL_FILE_DEPR'=>'_' 默认的模板文件就 ...

  7. ant design pro 表单

    1.Input Enter事件 <input onKeyUp={this.onKeyUp} onPressEnter={this.enter} /> onKeyUp = (e) => ...

  8. js 值和类型

    js中变量是没有类型的,只有值才有类型. 变量随时可以持有任何类型的值. <!DOCTYPE html> <html lang="zh"> <head ...

  9. 创建支持多种屏幕尺寸的apk

    文章转至:http://hell0android.iteye.com/blog/1899605 创建对两种以上屏幕尺寸的多apk支持(Creating Multiple APKs with 2+ Di ...

  10. WP8滑动条(Slider)控件的使用

    1. <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinit ...