【问题标题】:How to use scanf in linux kernel?如何在linux内核中使用scanf?
【发布时间】:2011-11-24 10:24:02
【问题描述】:

我是内核编程的新手。当我尝试在我的字符设备文件代码中使用 scanf 时,我收到此错误消息 :error: 函数‘scanf’的隐式声明

我该如何解决这个问题?请帮帮我。

我在虚拟机中使用 linux CentOS。

【问题讨论】:

  • 你不应该使用任何*scanf函数 -- 即使你没有在内核模式下编程。

标签: linux kernel scanf


【解决方案1】:

因为内核没有“标准输入”或“打开文件”,所以没有可用的scanf() 函数是没有意义的。 (好吧,BSD 进程记帐是内核确实打开了一个文件的地方。但是内核写入这个文件。)

您要查找的替换名称为sscanf()vsscanf(),两者都在lib/vsprintf.c 中定义:

/**
 * vsscanf - Unformat a buffer into a list of arguments
 * @buf:    input buffer
 * @fmt:    format of buffer
 * @args:   arguments
 */
int vsscanf(const char *buf, const char *fmt, va_list args)

/**
 * sscanf - Unformat a buffer into a list of arguments
 * @buf:    input buffer
 * @fmt:    formatting of buffer
 * @...:    resulting arguments
 */
int sscanf(const char *buf, const char *fmt, ...)

您选择哪一个取决于您更愿意如何称呼它。 sscanf() 的源代码显示了如何使用 vssanf() 函数,以防您更愿意使用可变参数调用约定。

【讨论】:

    【解决方案2】:

    您不能在内核中使用 libc 函数。仅限纯 C。我相信有人可以提供内核等价物。

    【讨论】:

    • 虽然这并没有完全错误,但内核中有许多函数与 libc 函数具有相同的名称和参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-24
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 2013-06-03
    相关资源
    最近更新 更多