- (call-with-input-file string proc [mode ...])procedure
- (call-with-output-file string proc [mode ...])procedure
String should be a string naming a file, and proc should be a procedure that accepts one argument. For call-with-input-file, the file should already exist; for call-with-output-file, the effect is unspecified if the file already exists. These procedures call proc with one argument: the port obtained by opening the named file for input or output. If the file cannot be opened, an error is signalled. If proc returns, then the port is closed automatically and the value(s) yielded by the proc is (are) returned. If proc does not return, then the port will not be closed automatically unless it is possible to prove that the port will never again be used for a read or write operation.
Rationale: Because Scheme's escape procedures have unlimited extent, it is possible to escape from the current continuation but later to escape back in. If implementations were permitted to close the port on any escape from the current continuation, then it would be impossible to write portable code using both call-with-current-continuation and call-with-input-file or call-with-output-file.
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.