Next: Custom Ports
Previous: String Ports
A port created by open-input-file, open-output-file,
process, and related function is a file-stream port.
The initial input, output, and error ports in stand-alone MzScheme
are also file-stream ports.
(file-stream-port? port) returns #t if the given port is a
file-stream port, #f otherwise.
Two special procedures work primarily on file-stream ports:
- (flush-output [output-port]) forces
all buffered data in the given file output port to be physically
written. Buffered data is automatically flushed after each
newline. The initial standard output and error ports are
automatically flushed when read, read-line,
read-string, or read-string-avail! are performed on the
initial standard input port. When called on a non-file-stream port,
flush-output takes no action. If output-port is omitted,
then the current output port is flushed.
- (file-position port) returns the current read/write
position of port, and (file-position port k) sets the
read/write position to k. The latter works only for file-stream
and string ports, and raises the exn:application:mismatch
exception for other port kinds. Calling file-position without a
position on a non-file/non-string input port returns the number of
characters that have been read from that port.
When (file-position port k) sets the position
k beyond the current size of an output file or string, the
file/string is enlarged to size k and the new region is filled
with #\nul. If k is beyond the end of an input file or
string, then reading thereafter returns eof without changing
the port's position.
Not all file-stream ports support setting the position. If
file-position is called with a position argument on such a
file-stream port, the exn:i/o:filesystem exception is raised.