将数据写入方案中的非现有文件(通过程序创建后)

时间:2012-05-04 00:26:49

标签: scheme lisp chicken-scheme

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

;;写入现有文件

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

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

1 个答案:

答案 0 :(得分:5)

尝试鸡计划:

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

或者对于Racket:

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