码迷,mamicode.com
首页 > 其他好文 > 详细

sscanf()的使用

时间:2020-12-01 11:51:35      阅读:2      评论:0      收藏:0      [点我收藏+]

标签:block   char*   style   plain   查找   div   form   scanf   命令行   

sscanf()的使用

函数原型:

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

...表示是一个多参函数,其实就和scanf是一样的,只是从命令行变成了从str中读取

 

针对于第二个const char*format参数有如下变化:

%s或%d跳过数据
%[width]s 读指定宽度的数据
%[a-z] 匹配a到z中任意字符(尽可能多的匹配)
%[aBc] 匹配a、B、c中一员,贪婪性
%a 匹配非a的任意字符,贪婪性
%a-z 表示读取除a-z以外的所有字符

 

具体实现代码如下

 

技术图片
 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 //sscanf的使用
 4 //%*s或%*d 跳过数据
 5 void test_tiaoguoNum()
 6 {
 7     const char *str = "1234asd5abcde";
 8     char buf[1024] = { 0 };
 9     sscanf(str,"%*d%s", buf);
10     printf("%s\n", buf);
11 }
12 //区间跳过%*[]
13 void test_tiaoguoSection()
14 {
15     const char* str = "asdbczs12345";
16     char buf[1024] = { 0 };
17     sscanf(str, "%*[a-z]%s", buf);
18     printf("%s\n", buf);
19 }
20 //指定读取宽度%[width]s
21 void test_SpecifiesWidthRead()
22 {
23     const char* str = "sadhjnkxz";
24     char buf[100] = { 0 };
25     sscanf(str, "%3s", buf);
26     printf("%s\n", buf);
27 }
28 //%[a-z] 匹配到a到z中任意字符
29 void test_MatchingRangeChar()
30 {
31     const char* str = "asdxzc1231a";
32     char buf[100] = { 0 };
33     sscanf(str, "%[a-z]s", buf);
34     printf("%s\n", buf);
35 }
36 //匹配具体的字符
37 //%[abc]
38 void test_Appoint() 
39 {
40     const char* str = "asdxzc1231a";
41     char buf[100] = { 0 };
42     sscanf(str, "%[asdx]", buf);
43     printf("%s\n", buf);
44 }
45 //匹配除了特殊字符以外的字符
46 void test_Matching_not()
47 {
48     const char* str = "asdxzc1231a";
49     char buf[100] = { 0 };
50     sscanf(str, "%[^d]", buf);
51     printf("%s\n", buf);
52 }
53 //匹配除了区间外的所有字符
54 void test_Matching_Not_Range()
55 {
56     const char* str = "asdxzc1231a";
57     char buf[100] = { 0 };
58     sscanf(str, "%[^0-9]", buf);
59     printf("%s\n", buf);
60 }
61 void Section_ip()
62 {
63     const char* str = "172.0.0.1";
64     int num1, num2, num3, num4;
65     sscanf(str, "%d.%d.%d.%d", &num1, &num2, &num3, &num4);
66     printf("%d %d %d %d", num1, num2, num3, num4);
67 }
68 //练习获取指定字符串
69 void test_ex1()
70 {
71     const char* str = "sadasd#zhangtao@1234";
72     char buf[100] = { 0 };
73     sscanf(str, "%*[^#]#%[^@]", buf);
74     printf("%s\n",buf);
75 }
76 void test_ex2()
77 {
78     //匹配出myname字符串
79     const char* str = "123abcd$myname@DDDqwe";
80     char buf[100] = { 0 };
81     sscanf(str, "%*[^$]$%[^@]", buf);
82     printf("%s\n", buf);
83 }
84 //只要有一个匹配失败就不会再匹配了
85 
86 int main(void)
87 {
88     //test_tiaoguoSection();
89     //test_SpecifiesWidthRead();
90     //test_MatchingRangeChar();
91     //test_Matching_not();
92     //test_Matching_Not_Range();
93     //Section_ip();
94     //test_ex1();
95     test_ex2();
96     return 0;
View Code
技术图片
 1 //查找字串
 2 #define _CRT_SECURE_NO_WARNINGS
 3 #include<stdio.h>
 4 
 5 
 6 int Get_son_string(char* str, char* Tarstr)
 7 {
 8     while (*str != \0)
 9     {
10         if (*str != *Tarstr)//判断能否匹配上子串的第一个数
11         {
12             str++;
13             continue;
14         }
15         char* temp1= ++str, * temp2=++Tarstr;//如果第一个匹配成功
16         while (1)
17         {
18             if (*temp2 == \0)    //如果字串取完说明匹配成功
19             {
20                 return 1;
21             }
22             if (*temp1 != *temp2)    //如果不等于就退出循环
23             {
24                 break;
25             }
26             //如果下一个还是成功就继续后移
27             temp1++;
28             temp2++;
29         }
30     }
31     return 0;
32 
33 }
34 
35 void main(void)
36 {
37     char str[] = "sadasddnfaxzcz";
38     char Tarstr[] = "dnf";
39     int ret = Get_son_string(str, Tarstr);
40     if (ret == 1)
41     {
42         printf("有字串\n");
43     }
44     else
45     {
46         printf("没有这字串\n");
47     }
48 }
View Code

 

sscanf()的使用

标签:block   char*   style   plain   查找   div   form   scanf   命令行   

原文地址:https://www.cnblogs.com/beautiful7/p/14040104.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!