int sscanf(const char* str,const char* format,...); 功能: 从str指定字符串读取数据,并根据参数format字符串转并格式化数据 参数: str 指定字符串首地址 format 字符串格式,用法和scanf()一样 返回值: 成功 返回参数数目 失败 -1
| 格式 | 作用 |
|---|---|
| %*s或%*d | 跳过字符串或数字 |
| %[width]s | 读指定宽度数据 |
| %[a-z] | 匹配a到z中任意字符(尽可能多的匹配) |
| %[aBc] | 匹配a、B、c中一员 |
| %[^a] | 匹配非a的任意字符, |
| %[^a-z] | 表示读取除a-z以外所有字符 |
只要有一个匹配失败,后面就不会在匹配了
#include#include #include // 1、%*s或*d 跳过数据 void test01() { char* str = "1234abcd"; char buf[1024] = {0}; // %*d跳过数字,%s匹配的字符串放入buf中 sscanf(str,"%*d%s",buf); // %[width]s 读指定宽度的数据 sscanf(str,"%6s",buf); // %[a-z] sscanf(str,"%[a-z]",buf); // %[aBc] 匹配a、B、c中一员,贪婪性 sscanf(str,"%[aBc]",buf); printf("%sn",buf); } int main() { test01(); return 0; }



