我如何使用Racket创建一个文件,以便能够存储和编辑用户输入的数据,或者,例如,高分.我已经阅读了一些文档,但没有找到关于如何做到这一点的明确答案.
在2htdp/batch-io库中读取和写入文件有一些简单的函数:http://docs.racket-lang.org/teachpack/2htdpbatch-io.html.它们有些受限,因为它们只在与程序本身相同的目录中访问文件,但您可以执行以下操作:
(require 2htdp/batch-io)
(write-file "highscore.txt" "Alice 25\nBob 40\n")
将数据写入文件(\n表示换行符),然后
(read-lines "highscore.txt")
以字符串列表的形式返回文件的行.
该球拍指南对输入和输出的一章。在第一部分介绍了阅读和书写文件,结合实例。它说
文件:该
open-output-file函数打开一个文件进行写入,open-input-file打开一个文件进行读取。
Examples:
> (define out (open-output-file "data"))
> (display "hello" out)
> (close-output-port out)
> (define in (open-input-file "data"))
> (read-line in)
"hello"
> (close-input-port in)
如果文件已经存在,则
open-output-file默认引发异常。提供像#:exists 'truncate或 之类的选项#:exists 'update来重写或更新文件:
等等。