R5RS Scheme input-output: how to write / add text to the output file?

What is an easy way to output text to a file in a Scheme version compatible with R5RS? I am using MIT MEEP (which uses Scheme for scripting) and I want to output text to a file. I found the following other answers on Stackoverflow:

File I / O - Scheme

How to add to a file using a schema

Add line to existing text file [sic] in IronScheme

But they were not exactly what I was looking for.

+2
source share
1 answer

The answers of Charlie Martin, Ben Rudger and Vijay Matthew were very useful, but I would like to give an answer that is simple and clear for the new Schemers, just like me :)

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

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

What is the difference between \ r and \ n?

Hooray!

+2
source

All Articles