int scanf(const char *format, …);
从标准输入读入信息
int fscanf(FILE *stream,const char *format, …);
从stream指向的文件中读入信息
格式化输入sscanf(例)
1、取指定长度的字符串
char buf[512] = "";
sscanf("123456", "%3s", buf);
printf("%s\n", buf);
结果为:123
2、取到指定字符为止的字符串
sscanf("123456 abcdedf", "%[^ ]", buf);
printf("%s\n", buf);
结果为:123456
3、取仅包含指定字符集的字符串
sscanf("123abcABC", "%[1-9a-z]", buf);
printf("%s\n", buf);
结果为:123abc
4、取到指定字符集为止的字符串
sscanf("123abcABC", "%[^A-Z]", buf);
printf("%s\n", buf);
结果为:123abc
5、取两个字符之间的字符串
sscanf("abc#def@ghi", "%*[^#]#%[^@]", buf);
printf("%s\n", buf);
结果为:def
6、仅保留字符串““hello, world”中的world
sscanf(“hello, world”, "%*s%s", buf);
printf("%s\n", buf);
结果为:world
7、分隔字符串2012:08:18
int a, b, c;
sscanf("2012:08:18", "%d:%d:%d", &a, &b, &c);
8、分隔字符串2012:08:18 -2012:08:18
char buf1[100] = "", buf2[100] = "";
sscanf(“2010:08:18 -2012:08:18”,"%s -%s", buf1, buf2);
sscanf(“2010:08:18 -2012:08:18”,"%[0-9:] -%[0-9:]", buf1, buf2);
int printf(const char *format, …);
输出到标准输出
int fprintf(FILE *stream,const char *format, …);
输出到文件
int sprintf(char *buf, const char *format, …);
int snprintf(char *buf, size_t n,const char *format, …);
输出到buf指定的内存区域。
注意:
sprintf函数没有指定写入的字符数,可能会造成由buf指向的内存区域溢出。标准I/O库函数
-------------------------------------------------------sscanf----------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include
#include
#include
int main()
{
char buf[100]="/mnt/ramdisk/172.0.0.1_19700101022912028_无_无_030_1_0_0.jpg";
char array[5];
//取buf中的 030
sscanf(buf,"%*[^_ ]_%*[^_ ]_%*[^_ ]_%*[^_ ]_%[^_ ]",array);// %*[^_ ] _ 跳过 _ 不要_ %[^_ ] 取到_
sscanf(buf,"%*[^_ ]_%*[^_ ]_%*[^_ ]_%*[^_ ]_%3s",array);// %3s 取3个字符
printf("******%s\n",array);
return 0;
}