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.
Open the file whose name is filename, and return a port representing that file. The attributes of the port are determined by the modes 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. This 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.
In theory we could create read/write ports which were buffered in one direction only. However this isn't included in the current interfaces.
If a file cannot be opened with the access requested,
open-filethrows an exception.
Take a string str naming an existing file and return an input port capable of delivering characters from the file. If the file cannot be opened, signal an error.
Take a string str naming an output file to be created and return an output port capable of writing characters to a new file by that name. If the file cannot be opened, signal an error. If a file with the given name already exists, the effect is unspecified.
Call proc with an input port opened on the file str. The file must already exist. If the file cannot be opened, an error is signalled. If the procedure returns, then close the port automatically and return the value yielded by the procedure. If the procedure 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.
closed automatically unless it is possible to prove that the port will never again be used for a read or write operation.
Arrange input to be from file and call thunk with no arguments. The file must already exist. During the call,
current-input-portreturns the input port that is opened on file. When the thunk returns, close the port and restore the previous default. Return the value yielded by thunk. If an escape procedure is used to escape from the continuation of these procedures, behavior is implementation dependent.
Arrange for output to be written to file and call thunk with no arguments. The effect is unspecified if the file already exists. During the call,
current-output-portreturns the output port that is opened on file. When the thunk returns, close the port and restore the previous default. Return the value yielded by thunk. If an escape procedure is used to escape from the continuation of these procedures, behavior is implementation dependent.
Arrange for errors to be written to file and call thunk with no arguments. The effect is unspecified if the file already exists. During the call,
current-error-portreturns the error port that is opened on file. When the thunk returns, close the port and restore the previous default. Return the value yielded by thunk. If an escape procedure is used to escape from the continuation of these procedures, behavior is implementation dependent.
Return the port modes associated with the open port port. These are necessarily 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.
Change the filename associated with port to filename. Note that this does not change the port's source of data, but only the value that is returned by
port-filenameand reported in diagnostic output.