【问题标题】:where is the text printed by C printfC printf 打印的文本在哪里
【发布时间】:2015-06-12 08:53:51
【问题描述】:

我碰巧遇到了通过 cffi 从 SBCL 调用 C printf 函数的问题。问题是当我调用 printf 函数时,我找不到输出文本,只是在 REPL 上显示 printf 函数的返回值。但是当我退出 SBCL 时,输出文本会神奇地出现在终端上。

  $ sbcl
  * (ql:quickload :cffi)
  * (cffi:foreign-funcall "printf" :string "hello" :int)
  ;;=> 5
  * (quit)
  hello$ 

最后一行,"hello$" 表示当从 SBCL 退出时,文本 "hello" 出现在终端上,后面跟着 shell 提示符 "$"。那么 printf 将文本“hello”打印到哪里呢?

我在 *standard-output* 上尝试了 `finish-output'、`force-output' 但不起作用。

【问题讨论】:

  • 通常看起来像输出缓冲,你需要告诉 C 清空缓冲区。

标签: io common-lisp cffi


【解决方案1】:

问题在于 C 的 stdio 库有自己的缓冲,与 Lisp 无关。刷新输出需要你有一个指向 C 的 FILE *stdout 变量的指针。你可以像这样得到这个指针:

 (cffi:defcvar ("stdout" stdout) :pointer)

那么,在使用printf之后:

(cffi:foreign-funcall "fflush" :pointer stdout :int)

【讨论】:

  • 在 MacOS 的 SBCL 2.0.11 中对我不起作用。我收到错误消息Trying to access undefined foreign variable "stdout".。还有其他人吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-27
  • 1970-01-01
  • 2010-12-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多