|
ssanf() 函数和 scanf() 功能类似,它的原型为:
#include <stdio.h>
int sscanf(const char *str, const char *format, ...); 与 scanf() 从 stdin 流里获得数据不同的是,sscanf() 从第一个参数 str 字符串指针处获得相应内容。一般用法,str 所指向字符串内容根据不同的类别分别用空格分开,这样对于处理要读取内容也比较容易。如果处理没有使用空格分开的字符串,可以在 format 中用 “精简版” 的正则表达式进行处理。
测试代码:
#include <stdio.h>
int main (void)
{
int pid;
char a[10];
int num;
char buf[100] = "100 open 130";
sscanf(buf, "%d %s %d", &pid, a, &num );
printf("%d, %s, %d\n", pid, a, num);
return 0;
} 运行及输出:
beyes@linux-beyes:~/C/base> ./sscanf.exe
100, open, 130 |
|
|