版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/guanjianhe/article/details/81626242
sscanf()
#include <stdio.h>
int main()
{
char* str;
// 读入字符串
sscanf("12345","%s",str);
printf("%s\n",str);
// 输出:12345
// %[^a] 匹配非a的任意字符,并且停止读入,贪婪性
sscanf("12345+acc","%[^+]",str);
printf("%s\n",str);
//输出: 12345
// %[a-z] 表示匹配a到z中任意字符,贪婪性(尽可能多的匹配)
// 取到指定字符集为止的字符串。如在下例中,取遇到小写字母为止的字符串。
sscanf("12345+acc121","%[^a-z]",str);
printf("%s\n",str);
// 输出:12345+
// 取指定长度的字符串。如在下例中,取最大长度为4字节的字符串。
sscanf("123456","%4s",str);
printf("%s\n",str);
// 输出:1234
// 取仅包含指定字符集的字符串。如在下例中,取仅包含1到9和小写字母的字符串。
sscanf("123456abc789dedfBCDEF","%[1-9,a-z]",str);
printf("%s\n",str);
// 输出:123456abc789dedf
sscanf("123456abcdedfBCDEF","%[1-9,A-Z]",str);
printf("%s\n",str);
// 输出:123456
// 给定一个字符串iios/12DDWDFF@122,获取 / 和 @ 之间的字符串,先将 "iios/"过滤掉,
// 再将非'@'的一串内容送到str中
// ( %*d 和 %*s) 加了星号 (*) 表示跳过此数据不读入
sscanf("iios/12DDWDFF@122","%*[^/]/%[^@]",str);
printf("%s\n",str);
// 输出:12DDWDFF
return 0;
}
/*
12345
12345
12345+
1234
123456abc789dedf
123456
12DDWDFF
*/
sprintf()
#include <stdio.h>
#include <math.h>
int main()
{
char str[80];
sprintf(str, "Pi 的值 = %.6f", M_PI);
puts(str);
// 输出:Pi 的值 = 3.141593
sprintf(str,"%d",1232);
puts(str);
// 输出:1232
sprintf(str,"%d%s%c%.1f",12,"ilove",'u',131452.0f);
puts(str);
// 输出:12iloveu131452.0
return(0);
}
/*
Pi 的值 = 3.141593
1232
12iloveu131452.0
*/