R5RS Scheme输入输出:如何将文本写入/附加到输出文件?

时间:2016-02-04 02:30:50

标签: io scheme read-write r5rs meep

在符合R5RS的Scheme版本中,将文本输出到文件的简单方法是什么? 我使用MIT的MEEP(使用Scheme for scripting),我想将文本输出到文件。我在Stackoverflow上找到了以下其他答案:

File I/O operations - Scheme

How to append to a file using Scheme

Append string to existing textfile [sic] in IronScheme

但是,他们并不完全是我想要的。

1 个答案:

答案 0 :(得分:3)

Charlie Martin,Ben Rudgers和Vijay Mathew的回答非常有帮助,但我想给出一个简单易懂的答案,对于像我这样的新Schemers:)

; 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”事情的任何挥之不去的问题,请看以下答案:

What is the difference between \r and \n?

干杯!