Simple file I/O

October 28, 2009
by Lee Spector (lspector)

If you want to read things from files into Scheme, or to write things to files from Scheme, there are a lot of built in procedures to use and a lot of approaches you could follow, but the simplest idea is to read and write single Scheme objects, which could be arbitrarily big things if you use nested lists. Here’s an example:

(define data
  '((here are some symbols (and also "a string"))
    ((2009 10 20 11 34)
     "Numbers in the car of the list of which I'm the cadr.")
    (more stuff)))

(define save-data
  (lambda ()
    (with-output-to-file "myData.txt"
      (lambda ()
        (print data))
      #:exists 'replace)))

(define read-data
  (lambda ()
    (with-input-from-file "myData.txt"
      (lambda ()
        (set! data (read))))))


One Response to “Simple file I/O”

  1.   Code Immersion » Blog Archive » More complicated input from files Says:

    […] Simple file I/O […]

Leave a Reply

You must be logged in to post a comment.