Vij*_*hew 15
在任何符合R5RS的方案中读取/写入文件的最简单方法是:
;; Read a text file
(call-with-input-file "a.txt"
(lambda (input-port)
(let loop ((x (read-char input-port)))
(if (not (eof-object? x))
(begin
(display x)
(loop (read-char input-port)))))))
;; Write to a text file
(call-with-output-file "b.txt"
(lambda (output-port)
(display "hello, world" output-port))) ;; or (write "hello, world" output-port)
Run Code Online (Sandbox Code Playgroud)
Scheme具有端口概念,表示可以在其上执行I/O操作的设备.计划助理的大多数实现call-with-input-file,并call-with-output-file用文字磁盘文件,你可以放心地使用它们.