ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
例子 ~~~ #include <iostream> #include <stddef.h> #include <stdlib.h> #include <stdio.h> #include <string.h> // 跳过数据 %*s, %*d void test01() { char *str = "12345abcde"; char buf[1024] = {0}; sscanf(str, "%*d%s", buf); printf("buf:%s\n", buf); } void test02() { //忽略字符串到空格或者\t char *str = "abcde\t123456"; char buf[1024] = {0}; sscanf(str, "%*s%s", buf); printf("buf:%s\n", buf); } void test03() { // %[a-z] 匹配a到z中任意字符(尽可能多的匹配) char *str = "12345abcde"; char buf[1024] = {0}; sscanf(str, "%*d%[a-c]", buf); printf("buf:%s\n", buf); } void test04() { //%[aBc] 匹配a、B、c中一员,贪婪性 char *str = "aABbcde"; char buf[1024] = {0}; sscanf(str, "%[aAb]", buf); printf("buf:%s\n", buf); } void test05() { //%[^a] 匹配非a的任意字符,贪婪性 char *str = "aABbcde"; char buf[1024] = {0}; sscanf(str, "%[^c]", buf); printf("buf:%s\n", buf); } void test06() { char *str = "aABbcde12345"; char buf[1024] = {0}; sscanf(str, "%[^0-9]", buf); printf("buf:%s\n", buf); } void test07() { char *ip = "127.0.0.1"; int num1 = 0, num2 = 0, num3 = 0, num4 = 0; //把ip录入进去 sscanf(ip, "%d.%d.%d.%d", &num1, &num2, &num3, &num4); printf("num1:%d\n", num1); printf("num2:%d\n", num2); printf("num3:%d\n", num3); printf("num4:%d\n", num4); } void test08() { char *str = "abcde#12uiop@0plju"; char buf[1024] = {0}; sscanf(str, "%*[^#]#%[^@]", buf); printf("buf:%s\n", buf); } int main() { // test01(); // test02(); // test03(); // test04(); // test05(); // test06(); // test07(); test08(); getchar(); return 0; } ~~~