sscanf详解

名称:  sscanf() - 从一个字符串中读进与指定格式相符的数据.
语法:  int sscanf( string str, string fmt, mixed var1, mixed var2 ... );
用法:  以指定的格式 fmt 去解读字符串 str. fmt 中除了 %d 和 %s 以外, 亦可包含其他的字符串作为格式. 每一个 %d 或 %s 都对应一个参数, 按顺序为 var1, var2 ... %d 读入一个整数到参数中, 而 %s 读入一个字符串.
* 亦可用于格式中, (即 %*d 和 %*s) 加了星号 (*) 表示跳过此数据不读入. (也就是不把此数据读入参数中)

1. 常见用法。
char buf[512] = ;
sscanf("123456 ", "%s", buf);
printf("%s/n", buf);
结果为:123456
2. 取指定长度的字符串。如在下例中,取最大长度为4字节的字符串。
sscanf("123456 ", "%4s", buf);
printf("%s/n", buf);
结果为:1234
3. 取到指定字符为止的字符串。如在下例中,取遇到空格为止字符串。
sscanf("123456 abcdedf", "%[^ ]", buf);
printf("%s/n", buf);
结果为:123456
4. 取仅包含指定字符集的字符串。如在下例中,取仅包含1到9和小写字母的字符串。
sscanf("123456abcdedfBCDEF", "%[1-9a-z]", buf);
printf("%s/n", buf);
结果为:123456abcdedf
5. 取到指定字符集为止的字符串。如在下例中,取遇到大写字母为止的字符串。
sscanf("123456abcdedfBCDEF", "%[^A-Z]", buf);
printf("%s/n", buf);
结果为:123456abcdedf
6、给定一个字符串iios/12DDWDFF@122,获取 / 和 @ 之间的字符串,先将 "iios/"过滤掉,再将非'@'的一串内容送到buf中
sscanf("iios/12DDWDFF@122", "%*[^/]/%[^@]", buf);
printf("%s/n", buf);
结果为:12DDWDFF
7、给定一个字符串““hello, world”,仅保留world。(注意:“,”之后有一空格)
sscanf(“hello, world”, "%*s%s", buf);
printf("%s/n", buf);
结果为:world
%*s表示第一个匹配到的%s被过滤掉,即hello被过滤了
如果没有空格则结果为NULL。


下面简单做些解说:
%20[^#]#   最多读入20个字符,直到遇见定界符#,但不包含定界符
%f#        读入一个浮点数,直到遇见定界符#
%i#        读入一个整数,直到遇见定界符#
%20[^/n]   最多读入20个字符,忽略行尾的回车符

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

#include <stdio.h> 
#include <iostream>
int main() 
{ 
	char user_name[20],password[10];
	sscanf("wsf:123654", "%127[^:]:%127[^ ]", user_name, password);   //%[^:]:表示用:隔开
	printf( "%s/n", user_name );
	printf( "%s/n", password);

	const char* s = "iios/12DDWDFF@122"; 
	char buf[20],buf1[10],buf3[20]; 
	int m;
	double df;
	sscanf( s, "%*[^/]/%[^@]", buf );     //%*[^/]表示跳过此数据不读入
	printf( "%s/n", buf ); 

	char sztime1[16] = "", sztime2[16] = ""; 
	sscanf("2006:03:18 - 2006:04:18", "%s - %s", sztime1, sztime2); 

	// sscanf("hello world", "%s %s", buf,buf1); 
	sscanf("hello ,world ,345,1.56", "%[^,],%[^,],%d[^,]", buf,buf1,&m);    //%[^,],表示用,隔开
	printf("%s/n", buf); 
	printf("%s/n", buf1); 
	printf( "%d/n", m);

	char hh[10],tt[10];
	int a,b,c; 
	sscanf("2006:03:18", "%[^:]:%[^:]:%d", hh, tt, &c);   //%[^:]:表示用:隔开
	//  a=atoi(temp); 
	//printf( "%d b is %d/n", c,b); 

	char str1[20];
	char str2[20];
	char str3[20];
	char str4[20];
	char str5[20];
	char str6[20];
	char str7[20];
	int a1,a2,a3,a4,a5,a6; 
	char log[]="<14>2002-11-11 12:12:12 11.22.33.44 3 3 aaaa aaaaaa"; 
	sscanf(log,"<%d>%s %s %s %d %d %s",&a1,str2,str3,str4,&a5,&a6,str7);
	printf("%d/n",a1);
	printf("%s/n",str2);
	printf("%s/n",str3);
	printf("%s/n",str4);
	printf("%d/n",a5);
	printf("%d/n",a6);
	printf("%s/n",str7);

	char test[]="<1111> 22";
	sscanf(test,"<%d> %d",&a5,&a6);
	printf("%d/n",a5);
	printf("%d/n",a6);

	sscanf(log,"<%[^>]>%[^ ] %[^ ] %[^ ] %[^ ] %[^ ] %[^$]",str1,str2,str3,str4,str5,str6,str7);
	printf("%s/n",str1);
	printf("%s/n",str2);
	printf("%s/n",str3);
	printf("%s/n",str4);
	printf("%s/n",str5);
	printf("%s/n",str6);
	printf("%s/n",str7);
	system("pause");
	return 0; 
} 


 

typedef map<int, int> templatemap;
templatemap LastWeekSort;
inline void PutInt(string &str,int data)
{
	char szBuffer[1024] = {0};
	sprintf(szBuffer,"%d",data );
	str += szBuffer;
	str += FIELD_NULL;
}

inline int String2int(string stc)
{
	int nAttribute;
	sscanf(stc.c_str(),"%d", &nAttribute);
	return nAttribute;
};

string  GetDetails(templatemap Sort)
{
	string str="";
	for (templatemap::iterator iter = Sort.begin(); iter!= Sort.end(); iter++)
	{
		PutInt(str, iter->first);
		PutInt(str, iter->second);
	}
	//Encode(str);
	return str;
}

bool  LastWeekLoadCompress(char *pValue)
{
	vector<string> vectTmpRecords;
	//Decode(pValue,vectTmpRecords);
	int nsize = vectTmpRecords.size();
	for (int i = 0; i < nsize; i += 2)
	{
		int nRoleID = String2int(vectTmpRecords[i]);
		int nScore = 0;
		if (i + 1 < nsize)
		nScore = String2int(vectTmpRecords[i + 1]);
		LastWeekSort[nRoleID]=nScore;
	}
	return true;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值