字符串修饰符在 kernel.h 中的 sscanf 中不起作用
String modifiers not working in sscanf in kernel.h
#include <linux/kernel.h> //sscanf
int err;
char a[32];
char b[32];
char c[32];
char test[20]="add abc de";
char *p=test;
err=sscanf(p,"%s %[^\t\n] %s",a,b,c);
printk("%d Data correctly parsed %s %s %s",err,a,b,c);
它打印以下内容而不是数组中的字符串。
\xfffffff4sa\xffffff82\xffffffff\xffffffff\xffffffff\xffffffff
问题出在第二个修饰符上,如果我使用普通 %s
就可以了。我只想将两个单词之间的所有单词存储在一个字符串中。
例如delete a b c fromTable
将 a b c
存储在一个字符串中。
上面的代码适用于 C 库中的 sscanf,但不适用于 kernel.h
函数sscanf
returns匹配的项目数。返回值 1 表示只分配了第一个参数 - a
。
仅在 4.6 版本中出现了对 %[...]
说明符的支持:https://elixir.bootlin.com/linux/v4.6-rc1/source/lib/vsprintf.c#L2736。他们提供以下警告:
/*
* Warning: This implementation of the '[' conversion specifier
* deviates from its glibc counterpart in the following ways:
* (1) It does NOT support ranges i.e. '-' is NOT a special
* character
* (2) It cannot match the closing bracket ']' itself
* (3) A field width is required
* (4) '%*[' (discard matching input) is currently not supported
*
* Example usage:
* ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
* buf1, buf2, buf3);
* if (ret < 3)
* // etc..
*/
除其他事项外,此警告表示说明符 %[..]
需要 字段宽度。在您的代码中,您没有提供该宽度,因此无法解析参数 b
。
#include <linux/kernel.h> //sscanf
int err;
char a[32];
char b[32];
char c[32];
char test[20]="add abc de";
char *p=test;
err=sscanf(p,"%s %[^\t\n] %s",a,b,c);
printk("%d Data correctly parsed %s %s %s",err,a,b,c);
它打印以下内容而不是数组中的字符串。
\xfffffff4sa\xffffff82\xffffffff\xffffffff\xffffffff\xffffffff
问题出在第二个修饰符上,如果我使用普通 %s
就可以了。我只想将两个单词之间的所有单词存储在一个字符串中。
例如delete a b c fromTable
将 a b c
存储在一个字符串中。
上面的代码适用于 C 库中的 sscanf,但不适用于 kernel.h
函数sscanf
returns匹配的项目数。返回值 1 表示只分配了第一个参数 - a
。
仅在 4.6 版本中出现了对 %[...]
说明符的支持:https://elixir.bootlin.com/linux/v4.6-rc1/source/lib/vsprintf.c#L2736。他们提供以下警告:
/*
* Warning: This implementation of the '[' conversion specifier
* deviates from its glibc counterpart in the following ways:
* (1) It does NOT support ranges i.e. '-' is NOT a special
* character
* (2) It cannot match the closing bracket ']' itself
* (3) A field width is required
* (4) '%*[' (discard matching input) is currently not supported
*
* Example usage:
* ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
* buf1, buf2, buf3);
* if (ret < 3)
* // etc..
*/
除其他事项外,此警告表示说明符 %[..]
需要 字段宽度。在您的代码中,您没有提供该宽度,因此无法解析参数 b
。