scanf sscanf 的用法
一、scanf函數(shù)
scanf() -?以屏幕(stdin)為輸入源,提取輸入指定格式的數(shù)據(jù),返回提取的數(shù)據(jù)個(gè)數(shù)。
函數(shù)原型:int scanf( const char *format [,argument]... );二、sscanf函數(shù)
sscanf() -?從一個(gè)字符串中讀進(jìn)與指定格式相符的數(shù)據(jù),非常適合字符獲取,例如提取IP,網(wǎng)址域名,郵箱用戶(hù)名等等。
函數(shù)原型:Int sscanf( string str, string format , mixed var1, mixed var2 ... );說(shuō)明:
其中的format可以是一個(gè)或多個(gè)?{%[*] [width] [{h | l | I64 | L}]type | ' ' | '/t' | '/n' |?非%符號(hào)}
注:
支持集合操作:
三、用法
3.1?最簡(jiǎn)單的用法
?? ?string = "china beijing 123";
?? ?ret = sscanf(string, "%s %s %d", buf1, buf2, &digit);
3.2 取指定長(zhǎng)度的字符串
?? ?string = "123456789";
?? ?sscanf(string, "%5s", buf1);
3.3 取到指定字符為止的字符串
?? ?string = "123/456";
?? ?sscanf(string, "%[^/]", buf1);
3.4 取到指定字符集為止的字符串
?? ?string = "123abcABC";
?? ?sscanf(string, "%[^A-Z]", buf1);
3.5 取僅包含指定字符集的字符串
?? ?string = "0123abcABC";
?? ?sscanf(string, "%[0-9]%[a-z]%[A-Z]", buf1, buf2, buf3);
3.6 獲取指定字符中間的字符串
?? ?string = "ios<android>wp7";
?? ?sscanf(string, "%*[^<]<%[^>]", buf1);
3.7 指定要跳過(guò)的字符串
?? ?string = "iosVSandroid";
?? ?sscanf(string, "%[a-z]VS%[a-z]", buf1, buf2);
3.8 分割以某字符隔開(kāi)的字符串
?? ?string = "android-iphone-wp7";
?? ?sscanf(string, "%[^-]-%[^-]-%[^-]", buf1, buf2, buf3);
3.9 提取郵箱地址
?? ?string = "Email:beijing@sina.com.cn";
?? ?sscanf(string, "%[^:]:%[^@]@%[^.].%s", buf1, buf2, buf3, buf4);?
3.10 過(guò)濾掉不想截取或不需要的字符串--補(bǔ)充,
?? ?string = "android iphone wp7";
?? ?sscanf(string, "%s %*s %s", buf1, buf2);
?
參考:
1:Linux C語(yǔ)言中sscanf 的詳細(xì)用法
總結(jié)
以上是生活随笔為你收集整理的scanf sscanf 的用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 吕梁离石学校计算机专业在哪里,山西吕梁计
- 下一篇: vb.net 教程 6-14 终止线程的