Chinaunix首页 | 论坛 | 博客
  • 博客访问: 72332
  • 博文数量: 17
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 280
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-04 09:30
文章分类

全部博文(17)

文章存档

2011年(1)

2009年(1)

2008年(15)

我的朋友

分类: C/C++

2008-04-25 18:30:27

下面这篇是网上转的,关于sscanf的用法。在解析AT的时候,挺有用的,特别是解析复杂情况时,可以借鉴下面的用法:
//----------------------------------------------
        sscanf(tokenstring, “%[^’,'],%[^’,'],%[^’,'],%s”, o, s, t, f);
//----------------------------------------------

很久以前,我以为c没有自己的split string函数,后来我发现了sscanf;一直以来,我以为sscanf只能以空格来界定字符串,现在我发现我错了。

sscanf是一个运行时函数,原形很简单:
int sscanf(
const char *buffer,
const char *format [,
argument ] …
);
它强大的功能体现在对format的支持上。

我以前用它来分隔类似这样的字符串2006:03:18:
int a, b, c;
sscanf(”2006:03:18″, “%d:%d:%d”, a, b, c);
以及2006:03:18 - 2006:04:18:
char sztime1[16] = “”, sztime2[16] = “”;
sscanf(”2006:03:18 - 2006:04:18″, “%s - %s”, sztime1, sztime2);

但是后来,我需要处理2006:03:18-2006:04:18
仅仅是取消了‘-’两边的空格,却打破了%s对字符串的界定。

我需要重新设计一个函数来处理这样的情况?这并不复杂,但是,为了使所有的代码都有统一的风格,我需要改动很多地方,把已有的sscanf替换成我自己的分割函数。我以为我肯定需要这样做,并伴随着对sscanf的强烈不满而入睡;一觉醒来,发现其实不必。

format-type中有%[]这样的type field。如果读取的字符串,不是以空格来分隔的话,就可以使用%[]。
%[]类似于一个正则表达式。[a-z]表示读取a-z的所有字符,[^a-z]表示读取除a-z以外的所有字符。

所以那个问题也就迎刃而解了:
sscanf(”2006:03:18 - 2006:04:18″, “%[0-9,:] - %[0-9,:]”, sztime1, sztime2);

在softmse (Jake) 的问题贴中 ,周星星给出了一个很cool的sscanf用例,而后通过学习,发现sscanf真棒,现做一总结。

原问题:

iios/12DDWDFF@122
获取/和@之间的字符串怎么做
C程序里面有什么函数吗?

周星星的代码:

#include

int main()
{
const char* s = “iios/12DDWDFF@122″;
char buf[20];

sscanf( s, “%*[^/]/%[^@]”, buf );
printf( “%s\n”, buf );

return 0;
}

结果为:12DDWDFF

sscanf与scanf类似,都是用于输入的,只是后者以屏幕(stdin)为输入源,前者以固定字符串为输入源。

函数原型:

int scanf( const char *format [,argument]… );
其中的format可以是一个或多个 {%[*] [width] [{h | l | I64 | L}]type | ‘ ‘ | ‘\t’ | ‘\n’ | 非%符号},

注:{a|b|c}表示a,b,c中选一,[d],表示可以有d也可以没有d。

width:宽度,一般可以忽略,用法如:

const char sourceStr[] = “hello, world”;

char buf[10] = {0};

sscanf(sourceStr, “%5s”, buf); //%5s,只取5个字符

cout << buf<< endl; 结果为:hello {h | l | I64 | L}:参数的size,通常h表示单字节size,I表示2字节 size,L表示4字节size(double例外),l64表示8字节size。 type :这就很多了,就是%s,%d之类。 特别的: %*[width] [{h | l | I64 | L}]type 表示满足该条件的被过滤掉,不会向目标参数中写入值。如: const char sourceStr[] = "hello, world"; char buf[10] = {0}; sscanf(sourceStr, "%*s%s", buf); //%*s表示第一个匹配到的%s被过滤掉,即hello被过滤了 cout << buf<< endl; 结果为:world 支持集合操作: %[a-z] 表示匹配a到z中任意字符,贪婪性(尽可能多的匹配) %[aB'] 匹配a、B、'中一员,贪婪性 %[^a] 匹配非a的任意字符,贪婪性 是不是感觉眼熟了啊,不错,这和正则表达式很相似,而且仍然支持过滤,即可以有%*[a-z].如: 星星大哥例子回顾: const char* s = "iios/12DDWDFF@122"; char buf[20]; sscanf( s, "%*[^/]/%[^@]", buf ); printf( "%s\n", buf ); 先将 "iios/"过滤掉,再将非'@'的一串内容送到buf中,cool.得到结果。 PS: 向星星大哥和softmse (Jake) 致谢了,有了你们才有它啊 大家都知道sscanf是一个很好用的函数,利用它可以从字符串中取出整数、浮点数和字符串等等。它的使用方法简单,特别对于整数和浮点数来说。但新手可能并不知道处理字符串时的一些高级用法,这里做个简要说明吧。 1. 常见用法。 char str[512] = {0}; sscanf("123456 ", "%s", str); printf("str=%s\n", str); 2. 取指定长度的字符串。如在下例中,取最大长度为4字节的字符串。 sscanf("123456 ", "%4s", str); printf("str=%s\n", str); 3. 取到指定字符为止的字符串。如在下例中,取遇到空格为止字符串。 sscanf("123456 abcdedf", "%[^ ]", str); printf("str=%s\n", str); 4. 取仅包含指定字符集的字符串。如在下例中,取仅包含1到9和小写字母的字符串。 sscanf("123456abcdedfBCDEF", "%[1-9a-z]", str); printf("str=%s\n", str); 5. 取到指定字符集为止的字符串。如在下例中,取遇到大写字母为止的字符串。 sscanf("123456abcdedfBCDEF", "%[^A-Z]", str); printf("str=%s\n", str); 源代码一如下: #include
#include

char *tokenstring = “12:34:56-7890″;
char a1[3], a2[3], a3[3];
int i1, i2;

void main(void)
{
clrscr();
sscanf(tokenstring, “%2s:%2s:%2s-%2d%2d”, a1, a2, a3, &i1, &i2);
printf(”%s\n%s\n%s\n%d\n%d\n\n”, a1, a2, a3, i1, i2);
getch();
}

源代码二如下:
#include
#include

char *tokenstring = “12:34:56-7890″;
char a1[3], a2[3], a3[3];
int i1, i2;

void main(void)
{
clrscr();
sscanf(tokenstring, “%2s%1s%2s%1s%2s%1s%2d%2d”, a1, &a, a2, &a3, a3, &a, &i1, &i2);
printf(”%s\n%s\n%s\n%d\n%d\n\n”, a1, a2, a3, i1, i2);
getch();
}

源代码三如下:
#include
#include

char *tokenstring = “12:34:56-7890″;
char a1[3], a2[3], a3[3], a4[3], a5[3];
int i1, i2;

void main(void)
{
char a;

clrscr();
sscanf(tokenstring, “%2s%1s%2s%1s%2s%1s%2s%2s”, a1, &a, a2, &a3, a3, &a, a4, a5);
i1 =atoi(a4);
i2 =atoi(a5);

printf(”%s\n%s\n%s\n%d\n%d\n\n”, a1, a2, a3, i1, i2);
getch();
}

方法四如下(以实例说明,原理相同):
/* The following sample illustrates the use of brackets and the
caret (^) with sscanf().
Compile options needed: none
*/

#include
#include
#include

char *tokenstring = “first,25.5,second,15″;
int result, i;
double fp;
char o[10], f[10], s[10], t[10];

void main()
{
result = sscanf(tokenstring, “%[^’,'],%[^’,'],%[^’,'],%s”, o, s, t, f);
fp = atof(s);
i = atoi(f);
printf(”%s\n %lf\n %s\n %d\n”, o, fp, t, i);
}

阅读(1065) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~