scanf sscanf 的用法
一、scanf函數
scanf() -?以屏幕(stdin)為輸入源,提取輸入指定格式的數據,返回提取的數據個數。
函數原型:int scanf( const char *format [,argument]... );二、sscanf函數
sscanf() -?從一個字符串中讀進與指定格式相符的數據,非常適合字符獲取,例如提取IP,網址域名,郵箱用戶名等等。
函數原型:Int sscanf( string str, string format , mixed var1, mixed var2 ... );說明:
其中的format可以是一個或多個?{%[*] [width] [{h | l | I64 | L}]type | ' ' | '/t' | '/n' |?非%符號}
注:
支持集合操作:
三、用法
3.1?最簡單的用法
?? ?string = "china beijing 123";
?? ?ret = sscanf(string, "%s %s %d", buf1, buf2, &digit);
3.2 取指定長度的字符串
?? ?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 指定要跳過的字符串
?? ?string = "iosVSandroid";
?? ?sscanf(string, "%[a-z]VS%[a-z]", buf1, buf2);
3.8 分割以某字符隔開的字符串
?? ?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 過濾掉不想截取或不需要的字符串--補充,
?? ?string = "android iphone wp7";
?? ?sscanf(string, "%s %*s %s", buf1, buf2);
?
參考:
1:Linux C語言中sscanf 的詳細用法
總結
以上是生活随笔為你收集整理的scanf sscanf 的用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 吕梁离石学校计算机专业在哪里,山西吕梁计
- 下一篇: vb.net 教程 6-14 终止线程的