Next: String Ports, Up: Port Types
The following procedures are used to open file ports.
See also open, for an interface
to the Unix open
system call.
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).
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 setvbuf
- `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. On DOS systems the default text mode converts CR+LF in the file to newline for the program, whereas binary mode reads and writes all bytes unchanged. On Unix-like systems there is no such distinction, text files already contain just newlines and no conversion is ever made. The
b
flag is accepted on all systems, but has no effect on Unix-like systems.(For reference, Guile leaves text versus binary up to the C library,
b
here just addsO_BINARY
to the underlyingopen
call, when that flag is available.)If a file cannot be opened with the access requested,
open-file
throws an exception.In theory we could create read/write ports which were buffered in one direction only. However this isn't included in the current interfaces.
Open filename for input. Equivalent to
(open-file filename "r")
Open filename for output. Equivalent to
(open-file filename "w")
Open filename for input or output, and call
(
procport)
with the resulting port. Return the value returned by proc. filename is opened as peropen-input-file
oropen-output-file
respectively, and an error is signalled if it cannot be opened.When proc returns, the port is closed. If proc does not return (eg. 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.
Open filename and call
(
thunk)
with the new port setup as respectively thecurrent-input-port
,current-output-port
, orcurrent-error-port
. Return the value returned by thunk. filename is opened as peropen-input-file
oropen-output-file
respectively, and an error is signalled 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.
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.
Return the filename associated with port. This function returns the strings "standard input", "standard output" and "standard error" when called on the current input, output and error ports respectively.
port must be open,
port-filename
cannot be used once the port is closed.
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.