sscanf(buf,"mtd%d: %x %x \"%[^\"]\"\n",&mtdnum,&size,&erasesize,name);
在双引号表示的字符串中需要有 “ 本身,则在前面加上 \
执行上条语句之前,buf 中的内容例如:
mtd0: 00280000 00010000 “boot+cfg+linux”
执行sscanf函数之后:
mtdnum = 0
size = 280000
erasesize = 10000
name = boot+cfg+linux
其中, %[^\”] 对应的内容就是:boot+cfg+linux
sscanf函数与scanf的比较:
函数原型:
int sscanf(const char *str, const char *format, ...);
sscanf与scanf类似,都是用于输入的,只是后者以键盘(stdin)为输入源,前者以固定字符串为输入源。其中,第一个形参 const char *str 是输入。
上网搜索得知 :
代码示例:
#include <stdio.h>
#include <string.h>
#define DBG(fmt, arg...) printf("[DBG:%s(%d) ]"fmt, __func__,__LINE__,##arg);
int main(int argc, char *argv[])
{
char buf_get[1024] = {0};
char buf_input[1024] = "mtd0: 00280000 00010000 \"boot+cfg+linux\"";
char buf_output[1024] = {0};
unsigned int mtdnum, size, erasesize;
scanf("%[^\"]", buf_get);
DBG("buf_get = %s\n", buf_get);
DBG("buf_input = %s\n", buf_input);
sscanf(buf_input,"mtd%d: %x %x \"%[^\"]\"\n",&mtdnum,&size,&erasesize,buf_output);
DBG("buf_output = %s\n", buf_output);
DBG("mtdnum = %x\n", mtdnum);
DBG("size = %x\n", size);
DBG("erasesize = %x\n", erasesize);
return 0;
}
代码执行如下:
下面的内容来自百度百科: