2

在符合 R5RS 的 Scheme 版本中将文本输出到文件的简单方法是什么?我使用麻省理工学院的 MEEP(它使用 Scheme 编写脚本),我想将文本输出到文件。我在 Stackoverflow 上找到了以下其他答案:

文件 I/O 操作 - 方案

如何使用 Scheme 追加到文件

将字符串附加到 IronScheme 中的现有文本文件 [原文如此]

但是,它们并不是我想要的。

4

1 回答 1

4

Charlie Martin、Ben Rudders 和 Vijay Mathew 的回答非常有帮助,但我想给出一个简单易懂的答案,对于像我这样的新计划者来说:)

; This call opens a file in the append mode (it will create a file if it doesn't exist)
(define my-file (open-file "my-file-name.txt" "a"))

; You save text to a variable
(define my-text-var1 "This is some text I want in a file")
(define my-text-var2 "This is some more text I want in a file")

; You can output these variables or just text to the file above specified
; You use string-append to tie that text and a new line character together.
(display (string-append my-text-var1 "\r\n" my-file))
(display (string-append my-text-var2 "\r\n" my-file))
(display (string-append "This is some other text I want in the file" "\r\n" my-file))

; Be sure to close the file, or your file will not be updated.
(close-output-port my-file)

而且,对于关于整个 "\r\n" 事情的任何挥之不去的问题,请参阅以下答案:

\r 和 \n 有什么区别?

干杯!

于 2016-02-04T02:30:49.617 回答