chickadee » scheme » with-output-to-file

(with-input-from-file string thunk [mode ...])procedure
(with-output-to-file string thunk [mode ...])procedure

String should be a string naming a file, and thunk should be a procedure of no arguments. For with-input-from-file, the file should already exist; for with-output-to-file, the effect is unspecified if the file already exists. The file is opened for input or output, an input or output port connected to it is made the default value returned by current-input-port or current-output-port (and is used by (read), (write obj), and so forth), and the thunk is called with no arguments. When the thunk returns, the port is closed and the previous default is restored. With-input-from-file and with-output-to-file return(s) the value(s) yielded by thunk. If an escape procedure is used to escape from the continuation of these procedures, their behavior is implementation dependent.

Additional mode arguments can be passed in, which should be any of the keywords #:text, #:binary or #:append. #:text and #:binary indicate the mode in which to open the file (this has an effect on non-UNIX platforms only), while #:append indicates that instead of truncating the file on open, data written to it should be appended at the end (only for output files). The extra mode arguments are CHICKEN extensions to the R5RS standard.