R5RS Scheme输入输出:如何将文本写入/附加到输出文件? [英] R5RS Scheme input-output: How to write/append text to an output file?

查看:182
本文介绍了R5RS Scheme输入输出:如何将文本写入/附加到输出文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What is a simple way to output text to file in a R5RS compliant version of Scheme? I use MIT's MEEP (which uses Scheme for scripting) and I want to output text to file. I have found the following other answers on Stackoverflow:

文件I / O操作 - 方案

如何使用Scheme附加到文件

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

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

But, they weren't exactly what I was looking for.

推荐答案

Charlie Martin,Ben Rudgers和Vijay Mathew的答案是非常有帮助,但我想给出一个简单易懂的答案,对于新的Schemers,比如我自己:)

The answers by Charlie Martin, Ben Rudgers, and Vijay Mathew were very helpful, but I would like to give an answer which is simple and easy to understand for new Schemers, like myself :)

; 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)

并且,对于任何挥之不去的问题整个\\ n的事情,请看以下答案:

And, for any lingering questions about the whole "\r\n" thing, please see the following answer:

\ r和\ n之间有什么区别?

干杯!

这篇关于R5RS Scheme输入输出:如何将文本写入/附加到输出文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆