sscanf的作用:从一个字符串中读进于指定格式相符的数据。利用它可以从字符串中取出整数、浮点数和字符串。
sscanf和scanf的区别:scanf是以键盘作为输入源,sscanf是以字符串作为输入源。
sscanf:
原型:
int sscanf(const char *str, const char *format,......);
说明:
sscanf()会将参数str的字符串根据参数format字符串来转换格式并格式化数据。转换后的结果存于对应的参数内。
成功则返回参数数目,失败则返回0
举例:
1. 取指定长度的字符串
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
sscanf("12345","%4s",str);
printf("%s\n",str);
return 0;
}
2. 格式化时间
#include<stdio.h>
#include<string.h>
int main()
{
int year, month, day, hour, minute, second;
sscanf("2013/02/13 14:55:34","%d/%d/%d %d:%d:%d",&year, &month, &day, &hour, &minute, &second);
printf("time=%d-%d-%d %d:%d:%d\n", year, month, day, hour, minute, second);
return 0;
}
3. 读入字符串
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
sscanf("12345","%s",str);
printf("%s\n",str);
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char str1[100], str2[100], str3[100];
gets(str1);
sscanf(str1,"%s%s",str2,str3);
printf("%s %s\n",str2,str3);
return 0;
}
4. %*d和%*s加了(*)表示跳过此数据不读入(也就是不把此数据读入参数中)
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
sscanf("1234abcd","%*d%s",str);
printf("%s\n",str);
return 0;
}
5. 取到指定字符为止的字符串。如例子所示,遇到‘+’为止的字符串。
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
sscanf("1234+abc","%[^+]",str);
printf("%s\n",str);
return 0;
}
遇到空格为止的字符串
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
sscanf("1234+abc1234","%[^]",str);
printf("str=%s\n",str);
return 0;
}
6. 取到指定字符集为止的字符串。如遇到小写字母为止的字符串。
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
sscanf("1234+abc1234","%[^a-z]",str);
printf("%s\n",str);
return 0;
}
7. 取仅包含指定字符集的字符串。(取仅包含数字和小写字母的字符串,是取得连续的字符串)。
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
sscanf("123456abcdefBFRGTY7890","%[1-9a-z]",str);
printf("%s\n",str);
return 0;
}
/*****************************************************
** Name : sscanf.c
** Author : gzshun
** Version : 1.0
** Date : 2011-12
** Description : sscanf function
******************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void sscanf_test(void);
static void sscanf_test(void)
{
int ret;
char *string;
int digit;
char buf1[255];
char buf2[255];
char buf3[255];
char buf4[255];
/*1.最简单的用法*/
string = "china beijing 123";
ret = sscanf(string, "%s %s %d", buf1, buf2, &digit);
printf("1.string=%s\n", string);
printf("1.ret=%d, buf1=%s, buf2=%s, digit=%d\n\n", ret, buf1, buf2, digit);
/*
**执行结果:
**1.ret=3, buf1=china, buf2=beijing, digit=123
**可以看出,sscanf的返回值是读取的参数个数
*/
/*2.取指定长度的字符串*/
string = "123456789";
sscanf(string, "%5s", buf1);
printf("2.string=%s\n", string);
printf("2.buf1=%s\n\n", buf1);
/*
**执行结果:
**2.buf1=12345
*/
/*3.取到指定字符为止的字符串*/
string = "123/456";
sscanf(string, "%[^/]", buf1);
printf("3.string=%s\n", string);
printf("3.buf1=%s\n\n", buf1);
/*
**执行结果:
**3.buf1=123
*/
/*4.取到指定字符集为止的字符串*/
string = "123abcABC";
sscanf(string, "%[^A-Z]", buf1);
printf("4.string=%s\n", string);
printf("4.buf1=%s\n\n", buf1);
/*
**执行结果:
**4.buf1=123abc
*/
/*5.取仅包含指定字符集的字符串*/
string = "0123abcABC";
sscanf(string, "%[0-9]%[a-z]%[A-Z]", buf1, buf2, buf3);
printf("5.string=%s\n", string);
printf("5.buf1=%s, buf2=%s, buf3=%s\n\n", buf1, buf2, buf3);
/*
**执行结果:
**5.buf1=123, buf2=abc, buf3=ABC
*/
/*6.获取指定字符中间的字符串*/
string = "ios<android>wp7";
sscanf(string, "%*[^<]<%[^>]", buf1);
printf("6.string=%s\n", string);
printf("6.buf1=%s\n\n", buf1);
/*
**执行结果:
**6.buf1=android
*/
/*7.指定要跳过的字符串*/
string = "iosVSandroid";
sscanf(string, "%[a-z]VS%[a-z]", buf1, buf2);
printf("7.string=%s\n", string);
printf("7.buf1=%s, buf2=%s\n\n", buf1, buf2);
/*
**执行结果:
**7.buf1=ios, buf2=android
*/
/*8.分割以某字符隔开的字符串*/
string = "android-iphone-wp7";
/*
**字符串取道'-'为止,后面还需要跟着分隔符'-',
**起到过滤作用,有点类似于第7点
*/
sscanf(string, "%[^-]-%[^-]-%[^-]", buf1, buf2, buf3);
printf("8.string=%s\n", string);
printf("8.buf1=%s, buf2=%s, buf3=%s\n\n", buf1, buf2, buf3);
/*
**执行结果:
**8.buf1=android, buf2=iphone, buf3=wp7
*/
/*9.提取邮箱地址*/
string = "Email:beijing@sina.com.cn";
sscanf(string, "%[^:]:%[^@]@%[^.].%s", buf1, buf2, buf3, buf4);
printf("9.string=%s\n", string);
printf("9.buf1=%s, buf2=%s, buf3=%s, buf4=%s\n\n", buf1, buf2, buf3, buf4);
/*
**执行结果:
**9.buf1=Email, buf2=beijing, buf3=sina, buf4=com.cn
*/
/*10.过滤掉不想截取或不需要的字符串--补充,
**在%号后面加一*号,代表过滤这个字符串,不读取
*/
string = "android iphone wp7";
sscanf(string, "%s %*s %s", buf1, buf2);
printf("10.string=%s\n", string);
printf("10.buf1=%s, buf2=%s\n\n", buf1, buf2);
/*
**执行结果:
**10.android wp7
*/
}
int main(int argc, char **argv)
{
sscanf_test();
return 0;
}
/*
**测试程序
**环境:
**Linux ubuntu 2.6.32-24-generic-pae #39-Ubuntu SMP Wed Jul 28 07:39:26 UTC 2010 i686 GNU/Linux
**gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
**
gzshun@ubuntu:~/c/sscanf$ gcc sscanf.c -o sscanf
gzshun@ubuntu:~/c/sscanf$ ./sscanf
1.string=china beijing 123
1.ret=3, buf1=china, buf2=beijing, digit=123
2.string=123456789
2.buf1=12345
3.string=123/456
3.buf1=123
4.string=123abcABC
4.buf1=123abc
5.string=0123abcABC
5.buf1=0123, buf2=abc, buf3=ABC
6.string=ios<android>wp7
6.buf1=android
7.string=iosVSandroid
7.buf1=ios, buf2=android
8.string=android-iphone-wp7
8.buf1=android, buf2=iphone, buf3=wp7
9.string=Email:beijing@sina.com.cn
9.buf1=Email, buf2=beijing, buf3=sina, buf4=com.cn
10.string=android iphone wp7
10.buf1=android, buf2=wp7
*/