
sscanf()的使用
View Code View Code
发布日期:2021-05-09 00:16:57
浏览次数:18
分类:博客文章
本文共 3525 字,大约阅读时间需要 11 分钟。
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 #include3 //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]s21 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;
1 //查找字串 2 #define _CRT_SECURE_NO_WARNINGS 3 #include4 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 else45 {46 printf("没有这字串\n");47 }48 }
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2025年03月26日 05时09分11秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
MongoDB 快速扫盲贴
2021-05-08
one + two = 3
2021-05-08
sctf_2019_easy_heap
2021-05-09
PyQt5之音乐播放器
2021-05-09
Redis进阶实践之十八 使用管道模式提高Redis查询的速度
2021-05-09
SQL注入
2021-05-09
MPI Maelstrom POJ - 1502 ⭐⭐ 【Dijkstra裸题】
2021-05-09
Problem 330A - Cakeminator (思维)
2021-05-09
LeetCode75 颜色分类 (三路快排C++实现与应用)
2021-05-09
C语言+easyX图形库的推箱子实现
2021-05-09
调试vs2019代码的流程
2021-05-09
脱壳与加壳-加壳-6-代码实现加密导入表
2021-05-09
Typora配置PicGo时,提示Failed to fetch
2021-05-09
bcolz的新操作
2021-05-09
zmq的send
2021-05-09
阿里钉钉面试题
2021-05-09
C++中找资源或者函数的方法
2021-05-09
delete对象时会自动调用类的析构函数
2021-05-09
POD类型
2021-05-09
const与常量,傻傻分不清楚~
2021-05-09