2016-02-04 66 views

回答

2

查理·马丁,本Rudgers,和维杰马修的答案是非常有益的,但我想给一个答案是简单和容易理解的有计划的新的,像我这样的:)

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

干杯!

+0

你测试过了吗?我相信你在陈列陈述中缺少一位父母。只要我们在这里。为什么不定义文本以包含换行符? – Rptx

+0

非常感谢大家围绕括号。当我写这些代码时,我在另一台机器上有这个代码,所以我错过了它们。关于“\ n”,确定你可以放入文本字​​符串,但是如果你试图追加一个函数的输出,这就更清楚了。 – ansebbian0