您的位置:首页 > 编程语言 > C语言/C++

C语言中的sscanf()函数使用详解

2015-08-16 18:03 961 查看

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

  Int sscanf( string str, string fmt, mixed var1, mixed var2 ... );
  int scanf( const char *format [,argument]... );

  说明:
  sscanf与scanf类似,都是用于输入的,只是后者以屏幕(stdin)为输入源,前者以固定字符串为输入源。
  其中的format可以是一个或多个 {%[*] [width] [{h | l | I64 | L}]type | ' ' | '\t' | '\n' | 非%符号}
  注:
  1、 * 亦可用于格式中, (即 %*d 和 %*s) 加了星号 (*) 表示跳过此数据不读入. (也就是不把此数据读入参数中)
  2、{a|b|c}表示a,b,c中选一,[d],表示可以有d也可以没有d。
  3、width表示读取宽度。
  4、{h | l | I64 | L}:参数的size,通常h表示单字节size,I表示2字节 size,L表示4字节size(double例外),l64表示8字节size。
  5、type :这就很多了,就是%s,%d之类。
  6、特别的:%*[width] [{h | l | I64 | L}]type 表示满足该条件的被过滤掉,不会向目标参数中写入值
  支持集合操作:
  %[a-z] 表示匹配a到z中任意字符,贪婪性(尽可能多的匹配)
  %[aB'] 匹配a、B、'中一员,贪婪性
  %[^a] 匹配非a的任意字符,贪婪性


format格式

{%[*] [width][{h | l | l64 | L}]type | ' ' | t' | '\n' | 非%符号}


注:

    *可用于格式中,(即%*d和%*s)加了星号(*)表示跳过此数据不读入。(也就是不把数据读入到参数中)
    width表示读取宽度
    {h | l | l64 | L}:参数size,通常h表示单字节size,l表示2字节size,L表示4字节size,l64表示8字节size
    type参数类型,例如%s,%d
    支持正则表达式,例如%[a-z]匹配a到z中任意字符(ps:正则表达式这个假期我会写一篇博客记录)

参考用例

   

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int result;
char str[100];
char buf1[255], buf2[255], buf3[255], buf4[255];
//基本用法
memset(str, 0, sizeof(str));
strcpy(str, "i love china!");
result = sscanf(str, "%s %s %s", buf1, buf2, buf3);
printf("%d\n%s\n%s\n%s\n", result, buf1, buf2, buf3);
/**
* 执行结果:
* 3
* i
* love
* china!
* 可以看出,sscanf的返回值为读取的参数个数
*/
//读取指定长度的字符串
memset(str, 0, sizeof(str));
strcpy(str, "abcdefghijklmnopq");
sscanf(str, "%5s", buf4);
printf("%s\n", buf4);
/**
* 执行结果:
* abcde
*/
//正则匹配字符串
memset(str, 0, sizeof(str));
memset(buf1, 0, sizeof(buf1));
memset(buf2, 0, sizeof(buf2));
memset(buf3, 0, sizeof(buf3));
strcpy(str, "123456abcdedfANDFS");
sscanf(str, "%[0-9]%[a-z]%[A-Z]", buf1, buf2, buf3);
printf("%s\n%s\n%s\n", buf1, buf2, buf3);
/**
* 执行结果:
* 123456
* abcdedf
* ANDFS
* 很难相信c语言竟然支持正则,不过c支持的正则挺弱的
*/
return 0;
}


九度ac题目
题目描述

    题目描述: 
    有一个网络日志,记录了网络中计算任务的执行情况,每个计算任务对应一条如下形式的日志记录: 
    “hs_10000_p”是计算任务的名称, 
    “2007-01-17 19:22:53,315”是计算任务开始执行的时间“年-月-日 时:分:秒,毫秒”,  
    “253.035(s)”是计算任务消耗的时间(以秒计) 
    hs_10000_p 2007-01-17 19:22:53,315 253.035(s) 
    请你写一个程序,对日志中记录计算任务进行排序。 
    时间消耗少的计算任务排在前面,时间消耗多的计算任务排在后面。 
    如果两个计算任务消耗的时间相同,则将开始执行时间早的计算任务排在前面。 
    输入: 
    日志中每个记录是一个字符串,每个字符串占一行。最后一行为空行,表示日志结束。日志中最多可能有10000条记录。 
    计算任务名称的长度不超过10,开始执行时间的格式是YYYY-MM-DD HH:MM:SS,MMM,消耗时间小数点后有三位数字。 
    计算任务名称与任务开始时间、消耗时间之间以一个或多个空格隔开,行首和行尾可能有多余的空格。 
    输出: 
    排序好的日志记录。每个记录的字符串各占一行。 
    输入的格式与输入保持一致,输入包括几个空格,你的输出中也应该包含同样多的空格。 
    样例输入: 
    hs_10000_p   2007-01-17 19:22:53,315     253.035(s) 
    hs_10001_p   2007-01-17 19:22:53,315     253.846(s) 
    hs_10002_m   2007-01-17 19:22:53,315     129.574(s) 
    hs_10002_p   2007-01-17 19:22:53,315     262.531(s) 
    hs_10003_m   2007-01-17 19:22:53,318     126.622(s) 
    hs_10003_p   2007-01-17 19:22:53,318     136.962(s) 
    hs_10005_m   2007-01-17 19:22:53,318     130.487(s) 
    hs_10005_p   2007-01-17 19:22:53,318     253.035(s) 
    hs_10006_m   2007-01-17 19:22:53,318     248.548(s) 
    hs_10006_p   2007-01-17 19:25:23,367    3146.827(s) 
    样例输出: 
    hs_10003_m   2007-01-17 19:22:53,318     126.622(s) 
    hs_10002_m   2007-01-17 19:22:53,315     129.574(s) 
    hs_10005_m   2007-01-17 19:22:53,318     130.487(s) 
    hs_10003_p   2007-01-17 19:22:53,318     136.962(s) 
    hs_10006_m   2007-01-17 19:22:53,318     248.548(s) 
    hs_10000_p   2007-01-17 19:22:53,315     253.035(s) 
    hs_10005_p   2007-01-17 19:22:53,318     253.035(s) 
    hs_10001_p   2007-01-17 19:22:53,315     253.846(s) 
    hs_10002_p   2007-01-17 19:22:53,315     262.531(s) 
    hs_10006_p   2007-01-17 19:25:23,367    3146.827(s) 


ac代码

  

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct mission
{
char str[200];
char name[20];
int year, month, day, hour, minute, second, micro;
double runtime;
};
int compare(const void *p, const void *q);
int main()
{
struct mission mis[10001];
int i, n = 0;
memset(mis, 0, sizeof(mis));
while(gets(mis
.str))
{
if(strcmp(mis
.str, "") == 0)
{
break;
}
sscanf(mis
.str, "%s%d-%d-%d %d:%d:%d,%d %lf", mis
.name, &mis
.year, &mis
.month, &mis
.day, &mis
.hour, &mis
.minute, &mis
.second, &mis
.micro, &mis
.runtime);
n ++;
}
qsort(mis, n, sizeof(mis[0]), compare);
for(i = 0; i < n; i ++)
{
printf("%s\n", mis[i].str);
}
return 0;
}
int compare(const void *p, const void *q)
{
const struct mission *a = p;
const struct mission *b = q;
if(a->runtime > b->runtime)
{
return 1;
}else if(a->runtime == b->runtime && a->year > b->year)
{
return 1;
}else if(a->runtime == b->runtime && a->year == b->year && a->month > b->month)
{
return 1;
}else if(a->runtime == b->runtime && a->year == b->year && a->month == b->month && a->day > b->day)
{
return 1;
}else if(a->runtime == b->runtime && a->year == b->year && a->month == b->month && a->day == b->day && a->hour > b->hour)
{
return 1;
}else if(a->runtime == b->runtime && a->year == b->year && a->month == b->month && a->day == b->day && a->hour == b->hour && a->minute > b->minute)
{
return 1;
}else if(a->runtime == b->runtime && a->year == b->year && a->month == b->month && a->day == b->day && a->hour == b->hour && a->minute == b->minute && a->second > b->second)
{
return 1;
}else if(a->runtime == b->runtime && a->year == b->year && a->month == b->month && a->day == b->day && a->hour == b->hour && a->minute == b->minute && a->second == b->second && a->micro > b->micro)
{
return 1;
}else if(a->runtime == b->runtime && a->year == b->year && a->month == b->month && a->day == b->day && a->hour == b->hour && a->minute == b->minute && a->second > b->second && a->micro == b->micro)
{
return 0;
}
else
{
return -1;
}
}

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C语言 sscanf