将输出打印到文件中还是不打印输出?

发布于 2024-12-12 22:27:21 字数 201 浏览 1 评论 0 原文

当我在 Lisp 中执行特定函数时,我想保存或忽略输出。我使用 Emacs 和 CCL。例如,

(defun foo (x) (format t "x = ~s~%" x))

如果我执行该函数,它会打印出“x = 5”。但我不想在缓冲区中打印输出,因为如果我有大量迭代,模拟速度就会降低。

有什么想法吗?

I'd like to save or ignore outputs when I execute a specific function in lisp. I use Emacs and CCL. For example,

(defun foo (x) (format t "x = ~s~%" x))

and if I execute the function, it prints out "x = 5". But I don't want to printout in a buffer, because if I have a large amount of iteration, the speed of simulation will be decreased.

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

佞臣 2024-12-19 22:27:21

您可以通过将 *standard-output* 绑定到流来临时重定向标准输出。例如,没有输出流的广播流将充当输出的黑洞

(let ((*standard-output* (make-broadcast-stream)))
  (foo 10)
  (foo 20))
;; Does not output anything.

:也可以使用其他绑定结构来执行此操作,例如 with-output-to-string 或 with-open-file:

(with-output-to-string (*standard-output*)
  (foo 10)
  (foo 20))
;; Does not print anything;
;; returns the output as a string instead.

(with-open-file (*standard-output* "/tmp/foo.txt" :direction :output)
  (foo 10)
  (foo 20))
;; Does not print anything;
;; writes the output to /tmp/foo.txt instead.

You can temporarily redirect standard output by binding *standard-output* to a stream. For example, a broadcast stream with no output streams will serve as a black hole for output:

(let ((*standard-output* (make-broadcast-stream)))
  (foo 10)
  (foo 20))
;; Does not output anything.

You can also do this with other binding constructs, such as with-output-to-string or with-open-file:

(with-output-to-string (*standard-output*)
  (foo 10)
  (foo 20))
;; Does not print anything;
;; returns the output as a string instead.

(with-open-file (*standard-output* "/tmp/foo.txt" :direction :output)
  (foo 10)
  (foo 20))
;; Does not print anything;
;; writes the output to /tmp/foo.txt instead.
叫嚣ゝ 2024-12-19 22:27:21

您可以为其提供一个输出文件流,而不是将 t 作为 format 的第一个参数,并且该语句的输出将发送到该文件流。

然而,过多的磁盘 I/O 也会增加运行时间,因此您可以考虑为程序设置两种模式,例如调试模式和发布模式,其中调试模式打印所有诊断消息,而发布模式则不打印任何内容。全部。

Instead of t as the first argument to format, you can give it an output file stream and your output for that statement will be sent to that file stream.

However having excessive disk I/O will also will increase your running time, hence you can consider having two modes like a debug and a release mode for your program where the debug mode prints all the diagnostic messages and the release mode does not print anything at all.

国际总奸 2024-12-19 22:27:21

我不确定我是否理解您的问题,但 format 的第二个参数是 。如果将其设置为t,它将打印到标准输出,但您也可以将其设置为打开的文件。

所以像这样的东西将允许您选择输出的位置:

;;output to file:
(setf *stream* (open "myfile" :direction :output
                              :if-exists :supersede)

;;alternative to output to standard output:
;;(setf *stream* t)

(defun foo (x) (format *stream* "x = ~s~%" x))

(foo 10)
(close *stream*) ;; only if output sent to a file

I'm not sure I understand your question, but the second argument to format is a stream. If you set it to t it prints to standard output, but you can also set it to an open file.

So something like this would allow you to select where the output goes:

;;output to file:
(setf *stream* (open "myfile" :direction :output
                              :if-exists :supersede)

;;alternative to output to standard output:
;;(setf *stream* t)

(defun foo (x) (format *stream* "x = ~s~%" x))

(foo 10)
(close *stream*) ;; only if output sent to a file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文