Next: , Up: Port Types   [Contents][Index]


6.14.10.1 File Ports

The following procedures are used to open file ports. See also open, for an interface to the Unix open system call.

All file access uses the “LFS” large file support functions when available, so files bigger than 2 Gbytes (2^31 bytes) can be read and written on a 32-bit system.

Most systems have limits on how many files can be open, so it’s strongly recommended that file ports be closed explicitly when no longer required (see Ports).

Scheme Procedure: open-file filename mode [#:guess-encoding=#f] [#:encoding=#f]
C Function: scm_open_file_with_encoding (filename, mode, guess_encoding, encoding)
C Function: scm_open_file (filename, mode)

Open the file whose name is filename, and return a port representing that file. The attributes of the port are determined by the mode string. The way in which this is interpreted is similar to C stdio. The first character must be one of the following:

r

Open an existing file for input.

w

Open a file for output, creating it if it doesn’t already exist or removing its contents if it does.

a

Open a file for output, creating it if it doesn’t already exist. All writes to the port will go to the end of the file. The "append mode" can be turned off while the port is in use see fcntl

The following additional characters can be appended:

+

Open the port for both input and output. E.g., r+: open an existing file for both input and output.

0

Create an "unbuffered" port. In this case input and output operations are passed directly to the underlying port implementation without additional buffering. This is likely to slow down I/O operations. The buffering mode can be changed while a port is in use (see Buffering).

l

Add line-buffering to the port. The port output buffer will be automatically flushed whenever a newline character is written.

b

Use binary mode, ensuring that each byte in the file will be read as one Scheme character.

To provide this property, the file will be opened with the 8-bit character encoding "ISO-8859-1", ignoring the default port encoding. See Ports, for more information on port encodings.

Note that while it is possible to read and write binary data as characters or strings, it is usually better to treat bytes as octets, and byte sequences as bytevectors. See Binary I/O, for more.

This option had another historical meaning, for DOS compatibility: in the default (textual) mode, DOS reads a CR-LF sequence as one LF byte. The b flag prevents this from happening, adding O_BINARY to the underlying open call. Still, the flag is generally useful because of its port encoding ramifications.

Unless binary mode is requested, the character encoding of the new port is determined as follows: First, if guess-encoding is true, the file-encoding procedure is used to guess the encoding of the file (see Character Encoding of Source Files). If guess-encoding is false or if file-encoding fails, encoding is used unless it is also false. As a last resort, the default port encoding is used. See Ports, for more information on port encodings. It is an error to pass a non-false guess-encoding or encoding if binary mode is requested.

If a file cannot be opened with the access requested, open-file throws an exception.

Scheme Procedure: open-input-file filename [#:guess-encoding=#f] [#:encoding=#f] [#:binary=#f]

Open filename for input. If binary is true, open the port in binary mode, otherwise use text mode. encoding and guess-encoding determine the character encoding as described above for open-file. Equivalent to

(open-file filename
           (if binary "rb" "r")
           #:guess-encoding guess-encoding
           #:encoding encoding)
Scheme Procedure: open-output-file filename [#:encoding=#f] [#:binary=#f]

Open filename for output. If binary is true, open the port in binary mode, otherwise use text mode. encoding specifies the character encoding as described above for open-file. Equivalent to

(open-file filename
           (if binary "wb" "w")
           #:encoding encoding)
Scheme Procedure: call-with-input-file filename proc [#:guess-encoding=#f] [#:encoding=#f] [#:binary=#f]
Scheme Procedure: call-with-output-file filename proc [#:encoding=#f] [#:binary=#f]

Open filename for input or output, and call (proc port) with the resulting port. Return the value returned by proc. filename is opened as per open-input-file or open-output-file respectively, and an error is signaled if it cannot be opened.

When proc returns, the port is closed. If proc does not return (e.g. if it throws an error), then the port might not be closed automatically, though it will be garbage collected in the usual way if not otherwise referenced.

Scheme Procedure: with-input-from-file filename thunk [#:guess-encoding=#f] [#:encoding=#f] [#:binary=#f]
Scheme Procedure: with-output-to-file filename thunk [#:encoding=#f] [#:binary=#f]
Scheme Procedure: with-error-to-file filename thunk [#:encoding=#f] [#:binary=#f]

Open filename and call (thunk) with the new port setup as respectively the current-input-port, current-output-port, or current-error-port. Return the value returned by thunk. filename is opened as per open-input-file or open-output-file respectively, and an error is signaled if it cannot be opened.

When thunk returns, the port is closed and the previous setting of the respective current port is restored.

The current port setting is managed with dynamic-wind, so the previous value is restored no matter how thunk exits (eg. an exception), and if thunk is re-entered (via a captured continuation) then it’s set again to the filename port.

The port is closed when thunk returns normally, but not when exited via an exception or new continuation. This ensures it’s still ready for use if thunk is re-entered by a captured continuation. Of course the port is always garbage collected and closed in the usual way when no longer referenced anywhere.

Scheme Procedure: port-mode port
C Function: scm_port_mode (port)

Return the port modes associated with the open port port. These will not necessarily be identical to the modes used when the port was opened, since modes such as "append" which are used only during port creation are not retained.

Scheme Procedure: port-filename port
C Function: scm_port_filename (port)

Return the filename associated with port, or #f if no filename is associated with the port.

port must be open; port-filename cannot be used once the port is closed.

Scheme Procedure: set-port-filename! port filename
C Function: scm_set_port_filename_x (port, filename)

Change the filename associated with port, using the current input port if none is specified. Note that this does not change the port’s source of data, but only the value that is returned by port-filename and reported in diagnostic output.

Scheme Procedure: file-port? obj
C Function: scm_file_port_p (obj)

Determine whether obj is a port that is related to a file.


Next: , Up: Port Types   [Contents][Index]