一、sscanf 函数介绍
头文件
#include <stdio.h>
原型:
int sscanf(const char *str, const char *format, ...);
返回:
On success, these functions return the number of input items success‐
fully matched and assigned; this can be fewer than provided for, or
even zero, in the event of an early matching failure.
The value EOF is returned if the endof input is reached before either
the firstsuccessful conversion or a matching failure occurs. EOF is
also returned ifa read error occurs, in which case the error indicator
forthe stream (see ferror(3)) is set, and errno is set to indicate the
error.
二、测试代码
#include <stdio.h>
#include <string.h>
int main (int argv,char* argc[])
{
char ageInfo []="age is 18";
char strInfo[]="info =name ;haha ;age ;18;\n";
char str1 [20];
char str2 [20];
char str3 [20];
char str4 [20];
int age;
sscanf (ageInfo,"%s %s %d",str1, str2,&age);
printf ("%s %s %d\n",str1, str2,age);
memset(str1,0,sizeof(str1));
memset(str2,0,sizeof(str2));
memset(str3,0,sizeof(str3));
memset(str4,0,sizeof(str4));
sscanf (strInfo,"%s =%s ;%s ;%s ;%d",str1, str2,str3,str4,&age);
printf ("%s %s %s %s %d\n",str1, str2,str3, str4,age);
return 0;
}
测试结果:
三、相似函数
int scanf(const char *format, ...);
int fscanf(FILE *stream, const char *format, ...);
int sscanf(const char *str, const char *format, ...);
输入来源不同,分别是(标准窗口、文件流、字符串):
The scanf() function reads input from the standard input stream stdin,
fscanf() reads input from the stream pointer stream,
sscanf() reads its input from the character string pointed to by str.
欢迎关注公众号:嵌入式学习与实践