C语言sscanf()函数详解的代码

下面资料是关于C语言sscanf()函数详解的内容,希望能对各位朋友有好处。

 




说明:sscanf()会将参数str的字符串根据参数format来转换并格式化数据。



format格式


 







#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main()
{
int result;
char str[100];
char buf1[255], buf2[255], buf3[255], buf4[255];

memset(str, 0, sizeof(str));
strcpy(str, "i love china!");
result = sscanf(str, "%s %s %s", buf1, buf2, buf3);
printf("%dn%sn%sn%sn", result, buf1, buf2, buf3);

memset(str, 0, sizeof(str));
strcpy(str, "abcdefghijklmnopq");
sscanf(str, "%5s", buf4);
printf("%sn", buf4);

memset(str, 0, sizeof(str));
memset(buf1, 0, sizeof(buf1));
memset(buf2, 0, sizeof(buf2));
memset(buf3, 0, sizeof(buf3));
strcpy(str, "123456abcdedfANDFS");
sscanf(str, "%[0-9]%[a-z]%[A-Z]", buf1, buf2, buf3);
printf("%sn%sn%sn", buf1, buf2, buf3);
return 0;
}









 

posted on 2019-01-17 14:19  whoamboys  阅读(1229)  评论(0编辑  收藏  举报

导航