1.头文件:#include<stdio.h>
2. 定义函数 int sscanf (const char *str,const char * format,........);
3.函数说明:
sscanf()会将参数str的字符串根据参数format字符串来转换并格式化数据。格式转换形式请参考scanf()。转换后的结果存于对应的参数内。
4.用途:一个字符串中读进与指定格式相符的数据。字符串操作是平常用途之多,截取,追加等等。也经常从文件中读取一行,取出所需要的字符串。基本有些是固定格式的。都可以用sscanf来得到。
其实就是format格式化字符串。下面就是经常用的小例子,练习一遍就能够十分熟悉使用了。对于广大网友来说还是非常有帮助性。
5实例:编译器vs2015
|
// 1.%s char buf[520] = {0}; sscanf("12qw..er", "%s", buf);// 从字符串“12qw..er”取%s 得到buf; printf("%s\n",buf);//buf = "12qw..er" |
|
// 2.取指定长度的字符串 sscanf("12qw..er", "%4s", buf); printf("%s\n", buf);// buf = "12qw" |
|
// 3.空格字符串 char str[10][256] = { 0 }; //memset(str, 0, sizeof(str)); sscanf("12 qw er", "%s", str[1]); printf("%s\n", str[1]);// str[1] = "12" // 说明遇到空格之间的就是%s memset(str, 0, sizeof(str)); sscanf("12 qw er", "%s%s%s", str[1],str[2],str[3]); printf("[1:%s,2:%s,3:%s]\n", str[1],str[2],str[3]); // str[1]= "12",str[2] = "qw",str[3]= "er" |
|
// 4.取到指定字符为止的字符串 // 空格 memset(str, 0, sizeof(str)); sscanf("1234a qwert789 p1234r", "%[^ ]", str[1]); printf("[1:%s]\n", str[1]);// str1 = "1234a" // 数字3 memset(str, 0, sizeof(str)); sscanf("1234a qwert789 p1234r", "%[^3]", str[1]); printf("[1:%s]\n", str[1]);// str1 = "12" // 字母p memset(str, 0, sizeof(str)); sscanf("1234a qwert789 p1234r", "%[^p]", str[1]); printf("[1:%s]\n", str[1]); // str1 = "1234a qwert789 " // 符号* memset(str, 0, sizeof(str)); sscanf("1234a qwert789 p*1234r", "%[^\*]", str[1]); printf("[1:%s]\n", str[1]); // str1 = "1234a qwert789 p" sscanf("1234a q*wert789 p*1234r", "%[^\*]", str[1]); printf("[1:%s]\n", str[1]); // str1 = "1234a q" // 符号" memset(str, 0, sizeof(str)); sscanf("1234a qwer\"t789 p*1234r", "%[^\"]", str[1]); printf("[1:%s]\n", str[1]); // str1 = "1234a qwer" memset(str, 0, sizeof(str)); sscanf("1234a q\"wert789 p*\"1234r", "%[^\"]%[^\"]", str[1],str[2]); printf("[1:%s,2:%s]\n", str[1],str[2]); // str1 = "1234a q" str2 = "" memset(str, 0, sizeof(str)); sscanf("1234a q\"wert789 p*\"1234r", "%[^\"]%s", str[1], str[2]); printf("[1:%s,2:%s]\n", str[1], str[2]); // str1 = "1234a q" str2 = "wert789" |
|
// 5.取仅包含指定字符集的字符串 // [a|b|C|&| |]:表示,从当前位置起,遇到[]里面的符号就输出。 // 一旦不是[]之间的字符就不输出 // 说明从开头取1-9和a-z之间的字符,遇到其余的字符就结束 memset(str, 0, sizeof(str)); sscanf("123456abcd789BCDfgPP", "%[1-9a-z]", str[1]); printf("[1:%s]\n", str[1]);// str1 = "123456abcd789" memset(str, 0, sizeof(str)); sscanf("123456abcd7%89BCDfgPP", "%[1-9a-z]", str[1]); printf("[1:%s]\n", str[1]);// str1 = "123456abcd7" // 说明遇见C,就停止输出了 memset(str, 0, sizeof(str)); sscanf("1234 abcd7%89BCDfgPP", "%[1-9| |a-z|%|B]", str[1]); printf("[1:%s]\n", str[1]);// str1 = "1234 abcd7%89B" // []可以多空格,但是必须1-9 A-Z这样连接起来不然呵呵哒了。 memset(str, 0, sizeof(str)); sscanf("1234 abcd7%89BCDFFfgPP", "%[ 1-9 | | a-z | % | B-D ]", str[1]); printf("[1:%s]\n", str[1]);// str1 = "1234 abcd7%89BCD" |
|
// 6. * 亦可用于格式中, (即 %*d 和 %*s) 加了星号 (*) 表示跳过此数据不读入. (也就是不把此数据读入参数中) memset(str, 0, sizeof(str)); sscanf("123456 abcd7 %89BCDfgPP", "%s%s", str[1], str[2]); printf("[1:%s,2:%s]\n", str[1], str[2]);// str1 = "123456" str2 = "abcd7" memset(str, 0, sizeof(str)); sscanf("123456 abcd7 %89BCDfgPP", "%*s%s", str[1]); printf("[1:%s\n", str[1]);// str1 = "abcd7" memset(str, 0, sizeof(str)); sscanf("123456 abcd7 %89BCDfgPP", "%*s%*s%s", str[1]); printf("[1:%s\n", str[1]);// str1 = "%89BCDfgPP" |
|
// 7.获取两符号之间的字符串 // 7.1字符串:"os/[email protected]" // 需要得到/和@之间的字符串,首先过滤os/,再将'@'的一串内容送到str中 memset(str, 0, sizeof(str)); sscanf("os/[email protected]", "%*[^/]/%[^@]", str[1]); printf("[1:%s]\n", str[1]);// str1 = "12DfgF" // 操作细化如下:%[^/]先取出/之前的内容,发现/和后面的字符连接在一起,所以需要去除 memset(str, 0, sizeof(str)); sscanf("os/[email protected]", "%[^/]%s", str[1], str[2]); printf("[1:%s,2:%s]\n", str[1],str[2]);// str1 = "os" str2="/[email protected]" memset(str, 0, sizeof(str)); sscanf("os/[email protected]", "%[^/]/%s", str[1], str[2]); printf("[1:%s,2:%s]\n", str[1], str[2]);// str1 = "os" str2="[email protected]" // %[^@]这个是取 直到@之前的内容 memset(str, 0, sizeof(str)); sscanf("os/[email protected]", "%[^/]%*[/]%[^@]", str[1], str[2]); printf("[1:%s,2:%s]\n", str[1], str[2]);// str1 = "os" str2="12DfgF" // 7.2字符串:"os [email protected]" // 取空格和@之间的字符串 // 1.空格作为分割%s%s分成两列,对第二个%s进行处理,空格直到@:%[^@] memset(str, 0, sizeof(str)); sscanf("os [email protected]", "%*s%s", str[1]); printf("[1:%s]\n", str[1]);// str1 = "[email protected]" memset(str, 0, sizeof(str)); sscanf("os [email protected]", "%*s%[^@]", str[1]); printf("[1:%s]\n", str[1]);// str1 = "12DfgF" // 7.3字符串:"@[email protected] : @[email protected] 122" // 取@和@之间的字符串 // 1.发现开头就有一个@符号,就需要过滤: [@]-->匹配当前位置是不是@ // 一看是@符号就需要过滤,所以需要过滤当前 %*[@] ,其后字符是"[email protected]" memset(str, 0, sizeof(str)); sscanf("@[email protected] : @[email protected] 122", "%*[@]%s", str[1]); printf("[1:%s]\n", str[1]);// str1 = "[email protected]" // 直到@符号为止:%[^@],ok了 memset(str, 0, sizeof(str)); sscanf("@[email protected] : @[email protected] 122", "%*[@]%[^@]", str[1]); printf("[1:%s]\n", str[1]);// str1 = "os" // 现在取出了os还有另外一组未取出@[email protected] // 先过滤@ : @这中间的符号,再直到@符号 memset(str, 0, sizeof(str)); sscanf("@[email protected] : @[email protected] 122", "%*[@]%[^@]%*[@]%*[^@]%s", str[1],str[2]); printf("[1:%s,2:%s]\n", str[1],str[2]);// str1 = "os" str2 = "@[email protected]" // 如今已经取出了"@[email protected]",过滤一个@符号,再直到@,就ok了 memset(str, 0, sizeof(str)); sscanf("@[email protected] : @[email protected] 122", "%*[@]%[^@]%*[@]%*[^@]%*[@]%[^@]", str[1], str[2]); printf("[1:%s,2:%s]\n", str[1], str[2]);// str1 = "os" str2 = "12DfgF" // 这样操作起来有点儿复杂多变,但是结果已经完美提取出来了。 // 有人说这不是有空格么 %*s 就可以跨越过去,然后提取@[email protected]不也行吗? memset(str, 0, sizeof(str)); sscanf("@[email protected] : @[email protected] 122", "%*[@]%[^@]%*s%s", str[1], str[2]); // 怎么会提取一个":"出来,我想要的是一个@[email protected]这个字符串呀。 // 其实%*[@]%[^@]:代表当前指向了@符号(可以简单认为下一个字符就是os后面的"@") // %*s 就是提取出来"@ "空格之间的“@”符号,下一个%s正好提取出来了“:” printf("[1:%s,2:%s]\n", str[1], str[2]);// str1 = "os" str2 = ":" // 好像你看到上面忽然恍然大悟了,再过滤掉空格之间的":",下一个字符串就是想要的字符串呀。 memset(str, 0, sizeof(str)); sscanf("@[email protected] : @[email protected] 122", "%*[@]%[^@]%*s%*s%s", str[1], str[2]); printf("[1:%s,2:%s]\n", str[1], str[2]);// str1 = "os" str2 = "@[email protected]" // 现在已经得到"%*[@]%[^@]" : 然后再次过滤掉@,直到@:得到12DfgF // 结果却不能如你想象的这样呀,怎么会取不出来呢,其实上面的%*s%s // %s代表了空格之间的字符,你用%*[@]来取就是取出来的是空格字符了。 memset(str, 0, sizeof(str)); sscanf("@[email protected] : @[email protected] 122", "%*[@]%[^@]%*s%*s%*[@]%[^@]", str[1], str[2]); printf("[1:%s,2:%s]\n", str[1], str[2]);// str1 = "os" str2 = "" // 过滤掉空格字符不就okl了 memset(str, 0, sizeof(str)); sscanf("@[email protected] : @[email protected] 122", "%*[@]%[^@]%*s%*s%*[ ]%*[@]%[^@]", str[1], str[2]); printf("[1:%s,2:%s]\n", str[1], str[2]);// str1 = "os" str2 = "@[email protected]" // 利用[ |@]来简化: {a|b|c}表示a,b,c中选一,[d],表示可以有d也可以没有d。 memset(str, 0, sizeof(str)); sscanf("@[email protected] : @[email protected] 122", "%*[@]%[^@]%*s%*s%*[ |@]%[^@]", str[1], str[2]); printf("[1:%s,2:%s]\n", str[1], str[2]);// str1 = "os" str2 = "@[email protected]" // 既然如此中间的":"也可以进行简化 memset(str, 0, sizeof(str)); sscanf("@[email protected] : @[email protected] 122", "%*[@]%[^@]%*[:| |@]%[^@]", str[1], str[2]); printf("[1:%s,2:%s]\n", str[1], str[2]);// str1 = "os" str2 = "@[email protected]" |
|
// 如下题: 从文件中读出一字符串: “"101aaa" "BBBB"\n” // 中文符号之间的都是字符串:写成程序:ch = "\"101aaa\" \"BBBB\""; // 得出引号之间的字符串: 这不也不是很难哈,利用上面那个简化式子一套入立马ok了。 memset(str, 0, sizeof(str)); sscanf("\"101aaa\" \"BBBB\"", "%*[\"]%[^\"]%*[\"| |]%[^\"]", str[1], str[2]); printf("[1:%s,2:%s]\n", str[1], str[2]);// str1 = "101aaa" str2 = "BBBB" // 字符串2:ch = "\"101 aad a\" \"BB B说的 个B\""; // 发现简化式未改变就已经满足要求了,有时候就需要固定格式的字符串: "" "" // "" ""这种非常的常见,但是这两者中间不能再有",不然就错误了。 memset(str, 0, sizeof(str)); sscanf("\"101 aad a\" \"BB B说的 个B\"", "%*[\"]%[^\"]%*[\"| |]%[^\"]", str[1], str[2]); printf("[1:%s,2:%s]\n", str[1], str[2]);// str1 = "101 aad a" str2 = "BB B说的 个" |