格式化字符串函数sscanf与swscanf
这两个函数用于获取格式化后的字符串。
#include<stdio.h>
int sscanf(const char* buffer, const char* format [, argument]...);
#include<stdio.h>或者#include<wchar.h>
int swscanf(const wchar_t* buffer, cosnt wchar_t* format [, argument]...);
Example:
#include <stdio.h>
void main()
{
char tokenstring[] = "15 12 14...";
char s[81];
char c;
int i;
float fp;
sscanf(tokenstring, "%s", s);
sscanf(tokenstring, "%c", &c);
sscanf(tokenstring, "%d", &i);
sscanf(tokenstring, "%f", &fp);
printf( "String = %s\n", s );
printf( "Character = %c\n", c );
printf( "Integer: = %d\n", i );
printf( "Real: = %f\n", fp );
}
Output:
String = 15
Character = 1
Integer: = 15
Real: = 15.000000