使用存在标志覆盖现有文件

using the exist flag to overwrite existing file

我目前正在阅读球拍文档的文件系统部分,我无法弄清楚如何使用存在标志,这是我写的:

(define (write-file file data) 
  (with-output-to-file file 
    (lambda () 
      (write data))
    #:exists (or/c 'error 'append 'update 'can-update 'replace 'truncate 'must-truncate 'truncate/replace)))))

文档没有提供示例用法,我是通过示例学习最好的人之一。

(or/c 'error 'append 'update 'can-update 'replace 
      'truncate 'must-truncate 'truncate/replace)))))

表示您需要选择其中一个标志。

例如:

 (define (write-file file data) 
   (with-output-to-file file 
     (lambda () 
       (write data))
     #:exists 'replace))

指南中充满了示例:http://docs.racket-lang.org/guide/ports.html

更新:

这是我试图解释如何阅读文档的尝试。 蓝色框的第一部分包含:

(open-output-file path               
     [  #:mode mode-flag                 
        #:exists exists-flag])      →       output-port?

表示函数open-output-file接受一个参数path return 和 output-port

(open-output-file "foo.txt") 将打开文件和 return 一个端口。 方括号表示可选参数。例如: (open-output-file "foo.txt" #:mode 'binary) 将以二进制模式打开文件。

文档说 #:mode mode-flag 所以 #:mode 之后的内容必须是合法的 mode-flag。在蓝色框的下方,它说:

  mode-flag   : (or/c 'binary 'text) = 'binary

这意味着 mode-flag 可以是 'binary'text

注意path参数也有解释:

  path        : path-string?

这意味着必须使用一个 path-string? 将 return 为真的值。 要查看这意味着什么,请单击 path-string? 并阅读有关路径字符串的信息。

最后一部分是 exists-flag 的列表。 or/c 意味着我们必须一次使用标志。

  exists-flag : (or/c 'error 'append 'update 'can-update
                      'replace 'truncate 'must-truncate 'truncate/replace)

另请注意,文档分为两部分:参考和指南。指南中有更多示例,请查看。