2016-02-04 254 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