gpt4 book ai didi

io - R5RS方案输入输出: How to write/append text to an output file?

转载 作者:行者123 更新时间:2023-12-03 09:18:29 26 4
gpt4 key购买 nike

在符合 R5RS 的方案版本中将文本输出到文件的简单方法是什么?我使用 MIT 的 MEEP(它使用 Scheme 进行脚本编写)并且我想将文本输出到文件。我在 Stackoverflow 上找到了以下其他答案:

File I/O operations - Scheme

How to append to a file using Scheme

Append string to existing textfile [sic] in IronScheme

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

最佳答案

Charlie Martin、Ben Rudgers 和 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”事件的任何挥之不去的问题,请参阅以下答案:

What is the difference between \r and \n?

干杯!

关于io - R5RS方案输入输出: How to write/append text to an output file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35191434/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com