sscanf的使用
- int sscanf(const char *str, const char *format,......);
#include<stdio.h>
int main1() {
char str[100];
sscanf("12345", "%4s", str);
printf("%s\n", str);
return 0;
}
int main2() {
int year, month, day, hour, minute, second;
//sscanf会自动去掉前面 中间 后面的空格
sscanf(" 2013/02/13 14:55:34", "%d/%d/%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
printf("time=%d-%d-%d %d:%d:%d\n", year, month, day, hour, minute, second);
return 0;
}
int main3() {
char str[100];
sscanf("12345", "%s", str);
printf("%s\n", str);
return 0;
}
int main4() {
char str1[100] = {"ab cd ef"};
char str2[100], str3[100];
sscanf(str1, "%s%s", str2, str3);
printf("%s %s\n", str2, str3);
return 0;
}
int main5() {
char str[100];
//%*d和%*s加了(*)表示跳过此数据不读入(也就是不把此数据读入参数中)
sscanf("1234abcd", "%*d%s", str);
printf("%s\n", str);
return 0;
}
int main6() {
char str[100];
sscanf("1234+abc", "%[^+]", str);
printf("%s\n", str);
return 0;
}
int main7() {
char str[100];
sscanf("1234+abc1234", "%[^a-z]", str);
printf("%s\n", str);
return 0;
}
int main8() {
char str[100];
sscanf("123456abcdefBFRGTY7890", "%[1-9a-z]", str);
printf("%s\n", str);
return 0;
}
int main() {
main1();
main2();
main3();
main4();
main5();
main6();
main7();
main8();
return 0;
}
运行结果:
1234
time=2013-2-13 14:55:34
12345
ab cd
abcd
1234
1234+
123456abcdef
sprintf的使用
标签:sscanf,int,char,sprintf,str,使用,return,100 From: https://www.cnblogs.com/clarencezzh/p/16950377.html