PREV UP NEXT SCM

4.6: Files and Ports

These procedures generalize and extend the standard capabilities in Ports.

Function: open-file string modes
Returns a port capable of receiving or delivering characters as specified by the modes string. If a file cannot be opened #f is returned.
Constant: open_read
Constant: open_write
Constant: open_both
Contain modes strings specifying that a file is to be opened for reading, writing, and both reading and writing respectively.
Function: _ionbf modestr
Returns a version of modestr which when open-file is called with it as the second argument will return an unbuffered port. A non-file input-port must be unbuffered in order for char-ready? to work correctly on it. The initial value of (current-input-port) is unbuffered if the platform supports it.
Function: close-port port
Closes port. The same as close-input-port and close-output-port.
Function: open-io-file filename
Function: close-io-port port
These functions are analogous to the standard scheme file functions. The ports are open to filename in read/write mode. Both input and output functions can be used with io-ports. An end of file must be read or a file-set-position done on the port between a read operation and a write operation or vice-versa.
Function: current-error-port
Returns the current port to which diagnostic output is directed.
Function: with-error-to-file string thunk
thunk must be a procedure of no arguments, and string must be a string naming a file. The file is opened for output, an output port connected to it is made the default value returned by current-error-port, and the thunk is called with no arguments. When the thunk returns, the port is closed and the previous default is restored. With-error-to-file returns the value yielded by thunk.
Function: with-input-from-port port thunk
Function: with-output-to-port port thunk
Function: with-error-to-port port thunk
These routines differ from with-input-from-file, with-output-to-file, and with-error-to-file in that the first argument is a port, rather than a string naming a file.
procedure: char-ready?
procedure: char-ready? port

Returns #t if a character is ready on the input port and returns #f otherwise. If char-ready? returns #t then the next read-char operation on the given port is guaranteed not to hang. If the port is at end of file then char-ready? returns #t. Port may be omitted, in which case it defaults to the value returned by current-input-port.

Rationale: Char-ready? exists to make it possible for a program to accept characters from interactive ports without getting stuck waiting for input. Any input editors associated with such ports must ensure that characters whose existence has been asserted by char-ready? cannot be rubbed out. If char-ready? were to return #f at end of file, a port at end of file would be indistinguishable from an interactive port that has no ready characters.