Next: Pipes Up: Ports Previous: Current Ports

## Opening File Ports

The `open-input-file` and `open-output-file` procedures accept an optional flag argument after the filename that specifies a mode for the file:

- `'binary` -- characters are returned from the port exactly as they are read from the file. Binary mode is the default mode.
- `'text` -- return and linefeed characters written and read from the file are filtered by the port in a platform specific manner:
  - Unix and BeOS: no filtering occurs.
  - Windows reading: a return-linefeed combination from a file is returned by the port as a single linefeed; no filtering occurs for return characters that are not followed by a linefeed, or for a linefeed that is not preceded by a return.
  - Windows writing: a linefeed written to the port is translated into a return-linefeed combination in the file; no filtering occurs for returns.
  - MacOS reading: a return character read from the file is returned as a linefeed by the port; no filtering occurs for linefeeds.
  - MacOS writing: a return character written to the port is translated into a linefeed in the file; no filtering occurs for linefeeds.

The `open-output-file` procedure can also take a flag argument that specifies how to proceed when a file with the specified name already exists:

- `'error` -- raise an exception (this is the default)
- `'replace` -- remove the old file and write a new one
- `'truncate` -- overwrite the old data
- `'truncate/replace` -- try `'truncate`; if it fails, try `'replace`
- `'append` -- append to the end of the file
- `'update` -- open an existing file without truncating it

If the `'update` flag is specified and the file does not exist, an exception is raised.

Extra flag arguments are passed to `open-output-file` in any order. Appropriate flag arguments can also be passed as the last argument(s) to `call-with-input-file`, `with-input-from-file`, `call-with-output-file`, and `with-output-to-file`. When conflicting flag arguments (e.g., both `'error` and `'replace`) are provided to `open-output-file`, `with-output-to-file`, or `call-with-output-file`, the `exn:application:mismatch` exception is raised.

Both `with-input-from-file` and `with-output-to-file` close the ports they create if control jumps out of the supplied thunk (either through a continuation or an exception). The port remains closed if control jumps back into the thunk.

When an input or output file-stream port is created, it is placed into the management of the current custodian (see section 9.5).

Next: Pipes Up: Ports Previous: Current Ports