2

我制作了一个简单的程序来将文本写入现有文件:

;; 写入现有文件

(define write-to-a-file
  (lambda (path txt)
    (call-with-output-file path
      (lambda (output-port)
        (write txt output-port)))))

但我想修改它,以便如果文件不存在,则应该创建它。如果文件存在,那么它应该写入文件而不删除文件的先前内容。我在写鸡计划。有任何想法吗?

4

1 回答 1

6

试试这个鸡计划:

(define (write-to-a-file path txt)
  (call-with-output-file path
    (lambda (output-port)
      (write txt output-port))
    #:append))

或者这个对于球拍:

(define (write-to-a-file path txt)
  (call-with-output-file path
    (lambda (output-port)
      (write txt output-port))
    #:exists 'append))
于 2012-05-04T01:16:46.213 回答