sscanf 的一些使用說明

2021-05-09 21:42:18 字數 4921 閱讀 9443

通過學習和使用個人認為,在字串格式不是很複雜,但是也並不簡單的時候用這個函式比較合適,這個尺度就要靠自己把握了,字串不是很複雜,但自己寫個處理的函式比較麻煩,效率也不高,就用這個函式,如果字串很複雜,那就用正規表示式吧。

不多說了,看看下面這些介紹和列子吧!

名稱:sscanf() - 從乙個字串中讀進與指定格式相符的資料.

函式原型:

int  sscanf( string str, string fmt, mixed var1, mixed var2 ... );

int  scanf( const char *format [,argument]... );

說明:sscanf與scanf類似,都是用於輸入的,只是後者以螢幕(stdin)為輸入源,前者以固定字串為輸入源。

其中的format可以是乙個或多個 ]type | ' ' | '/t' | '/n' | 非%符號}

支援集合操作:

%[a-z] 表示匹配a到z中任意字元,貪婪性(盡可能多的匹配)

%[ab'] 匹配a、b、'中一員,貪婪性

%[^a] 匹配非a的任意字元,貪婪性

例子: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。

8、char *s="1try234delete5" 

則: sscanf(s, "1%[^2]234%[^5]", s1, s2);

scanf的format中出現的非轉換字元(%之前或轉換字元之後的字元),即此例中的1234用來跳過輸入中的相應字元;

『』的含義與正規表示式中相同,表示匹配其中出現的字串行;^表示相反。使用[ ]時接收輸入的變數必須是有足夠儲存空間的char、signed char、unsigned char陣列。記住[也是轉換字元,所以沒有s了。

8、分割以某字元標記的字串。

char test="222,333,444,,,555,666";

char s1[4],s2[4],s3[4],s4[4],s5[4],s6[4],s7[4];

sscanf(test,"%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,]",s1,s2,s3,s4,s5,s6,s7);

printf("sssa1=%s",s1);

printf("sssa2=%s",s2);

printf("sssa3=%s",s3);

printf("sssa4=%s",s4);

printf("sssa5=%s",s5);

printf("sssa6=%s",s6);

printf("sssa7=%s",s7);

9、乙個提取使用者個人資料中郵件位址的例子

#include

#include

using namespace std;

int main() ;

char b[20]=;

//假設email位址資訊以';'結束

sscanf("email:[email protected];","%*[^:]:%[^;]",a);

//假設email位址資訊沒有特定的結束標誌

sscanf("email:[email protected]","%*[^:]:%s",b);

printf("%s/n",a);

printf("%s/n",b);

system("pause");

return 0; }

關鍵是"%*[^:]:%[^;]"和"%*[^:]:%s"這兩個引數的問題

%*[^:]    表示滿足""裡的條件將被過濾掉,不會向目標引數中寫入值。這裡的意思是在

第乙個':'之前的字元會在寫入時過濾掉,'^'是表示否定的意思,整個引數翻譯

成白話就是:將在遇到第乙個':'之前的(不為':'的)字元全部過濾掉。

:         自然就是跳過':'的意思。

%[^;]     拷貝字元直到遇到';'。

一下摘自:

%[ ] 的用法:%[ ]表示要讀入乙個字元集合, 如果[ 後面第乙個字元是」^」,則表示反意思。

[ ]內的字串可以是1或更多字元組成。空字符集(%)是違反規定的,可

導致不可預知的結果。%[^]也是違反規定的。

%[a-z] 讀取在 a-z 之間的字串,如果不在此之前則停止,如

char s="hello, my friend」 ;         // 注意: ,逗號在不 a-z之間

sscanf( s, 「%[a-z]」, string ) ; // string=hello

%[^a-z] 讀取不在 a-z 之間的字串,如果碰到a-z之間的字元則停止,如

char s="hellokitty」 ;         // 注意: ,逗號在不 a-z之間

sscanf( s, 「%[^a-z]」, string ) ; // string=hello

%*[^=]    前面帶 * 號表示不儲存變數。跳過符合條件的字串。

char s="notepad=1.0.0.1001" ;

char szfilename [32] = "" ;

int i = sscanf( s, "%*[^=]", szfilename ) ; // szfilename=null,因為沒儲存

int i = sscanf( s, "%*[^=]=%s", szfilename ) ; // szfilename=1.0.0.1001

%40c      讀取40個字元

the run-time

to the string, nor does reading 40 characters

automatically terminate the scanf() function. because the

library uses buffered input, you must press the enter key

to terminate the string scan. if you press the enter before

the scanf() reads 40 characters, it is displayed normally,

and the library continues to prompt for additional input

until it reads 40 characters

char s="notepad=1.0.0.1001" ;

char szfilename [32] = "" ;

int i = sscanf( s, "%[^=]", szfilename ) ; // szfilename=notepad     

如果引數格式是:%[^=:] ,那麼也可以從 notepad:1.0.0.1001讀取notepad

使用例子:

char s="notepad=1.0.0.1001" ;

char szname [32] = "" ;

char szver [32] = 「」 ;

sscanf( s, "%[^=]=%s", szname , szver ) ; // szname=notepad, szver=1.0.0.1001

總結:%有很大的功能,但是並不是很常用到,主要因為:

1、許多系統的 scanf 函式都有漏洞. (典型的就是 tc 在輸入浮點型時有時會出錯).

2、用法複雜, 容易出錯.

3、編譯器作語法分析時會很困難, 從而影響目標**的質量和執行效率.

個人覺得第3點最致命,越複雜的功能往往執行效率越低下。而一些簡單的字串分析我們可以自已處理。

C string類的一些使用說明

string類是 c 兩個預定義引用型別之一,是對字串的原生支援,在c 中極為常用,了解其特性和用法很有必要。string類表示的字串主要特點 string類是unicode utf 16 字元陣列 string類內容是不可更改的。string類很多方法表面看是在修改字串,實際只是生成新的字串副本 ...

關於本部落格的一些使用說明

陸續收到一些給我的郵件,說是感覺我的這個部落格裡面的文章比較多,分類做得不是特別好。這乙個確實是我以前沒有怎麼注意到,但現在要調整起來也不是很容易。近期寫的一些東西這方面有所注意,例如最新的隨筆系列有 實踐與思考 書籍 系列 moss 2010 visual studio 2010開發體驗 1.善加...

jstree 的一些爬坑指南使用說明

css庫 js庫 官方使用的容器為 接下來是 js 將 jstree 例項化 container jstree plugins contextmenu 外掛程式還有這些 按需使用 plugins checkbox contextmenu dnd massload search sort state ...