Next: , Previous: , Up: stdlib io port   [Index]


5.8.2.10 Output ports

An output port is a sink to which bytes or characters are written. The written data may control external devices or may produce files and other objects that may subsequently be opened for input.

Procedure: output-port? obj

Return #t if the argument is an output port (or a combined input and output port), #f otherwise.

Procedure: flush-output-port output-port

Flushes any buffered output from the buffer of output-port to the underlying file, device, or object. The flush-output-port procedure returns unspecified values.

Procedure: output-port-buffer-mode output-port

Return the symbol that represents the buffer mode of output-port.

Procedure: open-file-output-port filename
Procedure: open-file-output-port filename file-options
Procedure: open-file-output-port filename file-options buffer-mode
Procedure: open-file-output-port filename file-options buffer-mode maybe-transcoder

maybe-transcoder must be either a transcoder or #f.

The open-file-output-port procedure returns an output port for the named file.

The file-options argument, which may determine various aspects of the returned port, defaults to the value of (file-options).

The buffer-mode argument, if supplied, must be one of the symbols that name a buffer mode. The buffer-mode argument defaults to block.

If maybe-transcoder is a transcoder, it becomes the transcoder associated with the port.

If maybe-transcoder is #f or absent, the port will be a binary port and will support the port-position and set-port-position! operations. Otherwise the port will be a textual port, and whether it supports the port-position and set-port-position! operations is implementation–dependent (and possibly transcoder–dependent).

Procedure: open-bytevector-output-port
Procedure: open-bytevector-output-port maybe-transcoder

maybe-transcoder must be either a transcoder or #f.

The open-bytevector-output-port procedure returns two values: an output port and an extraction procedure. The output port accumulates the bytes written to it for later extraction by the procedure.

If maybe-transcoder is a transcoder, it becomes the transcoder associated with the port. If maybe-transcoder is #f or absent, the port will be a binary port and will support the port-position and set-port-position! operations. Otherwise the port will be a textual port, and whether it supports the port-position and set-port-position! operations is implementation–dependent (and possibly transcoder–dependent).

The extraction procedure takes no arguments. When called, it returns a bytevector consisting of all the port’s accumulated bytes (regardless of the port’s current position), removes the accumulated bytes from the port, and resets the port’s position.

Procedure: call-with-bytevector-output-port proc
Procedure: call-with-bytevector-output-port proc maybe-transcoder

proc must accept one argument. maybe-transcoder must be either a transcoder or #f.

The call-with-bytevector-output-port procedure creates an output port that accumulates the bytes written to it and calls proc with that output port as an argument.

Whenever proc returns, a bytevector consisting of all of the port’s accumulated bytes (regardless of the port’s current position) is returned and the port is closed.

The transcoder associated with the output port is determined as for a call to open-bytevector-output-port.

Procedure: open-string-output-port

Return two values: a textual output port and an extraction procedure. The output port accumulates the characters written to it for later extraction by the procedure.

The port may or may not have an associated transcoder; if it does, the transcoder is implementation-dependent. The port should support the port-position and set-port-position! operations.

The extraction procedure takes no arguments. When called, it returns a string consisting of all of the port’s accumulated characters (regardless of the current position), removes the accumulated characters from the port, and resets the port’s position.

Procedure: call-with-string-output-port proc

proc must accept one argument.

The call-with-string-output-port procedure creates a textual output port that accumulates the characters written to it and calls proc with that output port as an argument.

Whenever proc returns, a string consisting of all of the port’s accumulated characters (regardless of the port’s current position) is returned and the port is closed.

The port may or may not have an associated transcoder; if it does, the transcoder is implementation-dependent. The port should support the port-position and set-port-position! operations.

Procedure: standard-output-port
Procedure: standard-error-port

Return a fresh binary output port connected to the standard output or standard error respectively. Whether the port supports the port-position and set-port-position! operations is implementation–dependent.

Procedure: current-output-port
Procedure: current-error-port

These return default textual ports for regular output and error output. Normally, these default ports are associated with standard output, and standard error, respectively.

The return value of current-output-port can be dynamically re–assigned using the with-output-to-file procedure from the (rnrs io simple (6)) library. A port returned by one of these procedures may or may not have an associated transcoder; if it does, the transcoder is implementation–dependent.

Procedure: make-custom-binary-output-port id write! get-position set-position! close

Return a newly created binary output port whose byte sink is an arbitrary algorithm represented by the write! procedure. id must be a string naming the new port, provided for informational purposes only. write! must be a procedure and should behave as specified below; it will be called by operations that perform binary output.

Each of the remaining arguments may be #f; if any of those arguments is not #f, it must be a procedure and should behave as specified in the description of make-custom-binary-input-port.

(write! bytevector start count)

start and count will be non-negative exact integer objects, and bytevector will be a bytevector whose length is at least start+count.

The write! procedure should write up to count bytes from bytevector starting at index start to the byte sink. In any case, the write! procedure should return the number of bytes that it wrote, as an exact integer object.

Implementation responsibilities: The implementation must check the return values of write! only when it actually calls write! as part of an I/O operation requested by the program. The implementation is not required to check that write! otherwise behaves as described. If it does not, however, the behavior of the resulting port is unspecified.

Procedure: make-custom-textual-output-port id write! get-position set-position! close

Return a newly created textual output port whose byte sink is an arbitrary algorithm represented by the write! procedure. Id must be a string naming the new port, provided for informational purposes only. write! must be a procedure and should behave as specified below; it will be called by operations that perform textual output.

Each of the remaining arguments may be #f; if any of those arguments is not #f, it must be a procedure and should behave as specified in the description of make-custom-textual-input-port.

(write! string start count)

start and count will be non–negative exact integer objects, and string will be a string whose length is at least start+count.

The write! procedure should write up to count characters from string starting at index start to the character sink. In any case, the write! procedure should return the number of characters that it wrote, as an exact integer object.

The port may or may not have an associated transcoder; if it does, the transcoder is implementation–dependent.

Implementation responsibilities: The implementation must check the return values of write! only when it actually calls write! as part of an I/O operation requested by the program. The implementation is not required to check that write! otherwise behaves as described. If it does not, however, the behavior of the resulting port is unspecified.


Next: , Previous: , Up: stdlib io port   [Index]