-1
#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 中的代码

4

1 回答 1

0

该函数sscanf返回匹配的项目数。返回值 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无法解析该参数。

于 2021-01-29T21:50:13.503 回答