C语言的sscanf()函数小结

作用: 

从字符串读取格式化输入

函数声明:

int sscanf(const char *str, const char *format, ...)

 参数说明:

str  C 字符串,是函数检索数据的来源
format C 字符串,由空格字符、非空格字符 和 format 说明符组成,指定读入的格式
说明符形式为 [=%[*][width][modifiers]type=],具体如下
width 这指定了在当前读取操作中读取的最大字符数。
modifiers 为对应的附加参数所指向的数据指定一个不同于整型(针对 d、i 和 n)、无符号整型(针对 o、u 和 x)或浮点型(针对 e、f 和 g)的大小: h :短整型(针对 d、i 和 n),或无符号短整型(针对 o、u 和 x) l :长整型(针对 d、i 和 n),或无符号长整型(针对 o、u 和 x),或双精度型(针对 e、f 和 g) L :长双精度型(针对 e、f 和 g)
type 指定了要被读取的数据类型以及数据读取方式。包括c(单个字符:读取下一个字符),d(十进制整数),s(字符串。这将读取连续字符,直到遇到一个空格字符)

返回值

如果成功,该函数返回成功匹配和赋值的个数(以空字符分隔开的个数)。如果到达文件末尾或发生读错误,则返回 EOF。

注意:匹配对象以空字符分隔 (空格,回车,换行)

常用示例

1.常规读入

#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;

int main(){
	int ret;
	int digit;
	char buf1[255];
	char buf2[255];
	char buf3[255];

	char *string="hello tomomrrow! 123/456";
	ret = sscanf(string,"%s %s %d",buf1,buf2,&digit);
	cout<<string<<endl;
	cout<<"返回值ret="<<ret<<endl
		<<"buf1="<<buf1<<endl
		<<"buf2="<<buf2<<endl
		<<"digit="<<digit<<endl;
}

运行结果:

注意,为方便起见,下面示例代码将省略头文件的引入和变量声明,其所需头文件及变量同上

 

2.读取指定长度的字符串(%长度s)

	char *string="hello tomorrow! 123/456";
	ret = sscanf(string,"%4s",buf1);
	cout<<string<<endl;
	cout<<"返回值ret="<<ret<<endl
		<<"buf1="<<buf1<<endl;

运行结果:

注意:读取的长度小于等于成功匹配的第一个字符段长度,如上例中,若取前7位,只能读入hello,因为成功匹配的第一个字段最长就是hello。

 3.读取到指定字符为止的字符串(%[^该指定字符])

	char *string="hello tomorrow! 123/456";
	ret = sscanf(string,"%[^!]",buf1);
	cout<<string<<endl;
	cout<<"返回值ret="<<ret<<endl
		<<"buf1="<<buf1<<endl;

运行结果:

 4.读取到指定字符集为止(%[^字符集起始字符-字符集终止字符])

	char *string="hello tomorrow! 123/456";
	ret = sscanf(string,"%[^5-9]",buf1);
	cout<<string<<endl;
	cout<<"返回值ret="<<ret<<endl
		<<"buf1="<<buf1<<endl;

运行结果:

5.取仅包含指定字符集的字符串(%[起始字符-终止字符])

	char *string="hello TOMORROW 123456";
	ret = sscanf(string,"%[a-z] %[A-Z] %[0-9]",buf1,buf2,buf3);
	cout<<string<<endl;
	cout<<"返回值ret="<<ret<<endl
		<<"buf1="<<buf1<<endl
		<<"buf2="<<buf2<<endl
		<<"buf3="<<buf3<<endl;

运行结果:

 6.获取指定字符串中间的字符串(%*[^首指定字符]<%[^尾指定字符])

	char *string="hello<tomorrow>123456";
	ret = sscanf(string,"%*[^<]<%[^>]",buf1);
	cout<<string<<endl;
	cout<<"返回值ret="<<ret<<endl
		<<"buf1="<<buf1<<endl;

运行结果:

7.指定要跳过的字节(在两个说明符中间声明该字节)

	char *string="hello tomorrow 123456";
	ret = sscanf(string,"%[a-z] tomorrow %[0-9]",buf1,buf2);
	cout<<string<<endl;
	cout<<"返回值ret="<<ret<<endl
		<<"buf1="<<buf1<<endl
		<<"buf2="<<buf2<<endl;

运行结果: 

8.分割以某字符隔开的字符串(^加该字符,中括号后再跟一个该字符)

	char *string="hello,tomorrow,123456";
	ret = sscanf(string,"%[^,],%[^,],%[^,]",buf1,buf2,buf3);
	cout<<string<<endl;
	cout<<"返回值ret="<<ret<<endl
		<<"buf1="<<buf1<<endl
		<<"buf2="<<buf2<<endl
		<<"buf3="<<buf3<<endl;

运行结果:

9.过滤掉不想提取的字符串(%*s)

	char *string="hello tomorrow 123456";
	ret = sscanf(string,"%s %*s %s",buf1,buf2,buf3);
	cout<<string<<endl;
	cout<<"返回值ret="<<ret<<endl
		<<"buf1="<<buf1<<endl
		<<"buf2="<<buf2<<endl
		<<"buf3="<<buf3<<endl;

运行结果:

参考于以下链接:

1.https://blog.csdn.net/gzshun/article/details/7081736

2.http://www.runoob.com/cprogramming/c-function-sscanf.html

感谢!

猜你喜欢

转载自blog.csdn.net/li_l_il/article/details/84205506