sscanf用法
时间:2010-12-02 来源:bluesky2254
说明:函数功能是将一个字符串的内容按格式输入,遇到第一个空白字符会结束,可用于字符转数字,类似于atoi。
源码:
/* SSCANF.C: This program uses sscanf to read data items
* from a string named tokenstring, then displays them.
*/
#include <stdio.h>
void main( void )
{
char tokenstring[] = "15 12 14...";
char s[81];
char c;
int i;
float fp;
/* Input various data from tokenstring: */
sscanf( tokenstring, "%s", s );
sscanf( tokenstring, "%c", &c );
sscanf( tokenstring, "%d", &i );
sscanf( tokenstring, "%f", &fp );
/* Output the data read */
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
相关阅读 更多 +