Ports

Ports represent input and output devices. To Scheme, an input port is a Scheme object that can deliver data upon command, while an output port is a Scheme object that can accept data. Different port types operate on different data.

A textual port supports reading or writing of individual characters from or to a backing store containing characters using read-char and write-char below, and it supports operations defined in terms of characters, such as read and write.

A binary port supports reading or writing of individual bytes from or to a backing store containing bytes using read-u8 and write-u8 below, as well as operations defined in terms of bytes.

Ports can be used to access files, devices, and similar things on the host system on which the Scheme program is running.

(call-with-input-file string proc ) file library procedure
(call-with-output-file string proc ) file library procedure
It is an error if proc does not accept one argument.

These procedures obtain a textual port obtained by opening the named file for input or output as if by open-input-file or open-output-file. The port and proc are then passed to a procedure equivalent to call-with-port.

(input-port? obj ) procedure
(output-port? obj ) procedure
(textual-port? obj ) procedure
(binary-port? obj ) procedure
(port? obj ) procedure
These procedures return #t if obj is an input port, output port, textual port, binary port, or any kind of port, respectively. Otherwise they return #f.

(input-port-open? port ) procedure
(output-port-open? port ) procedure
Returns #t if port is still open and capable of performing input or output, respectively, and #f otherwise.

(current-input-port ) procedure
(current-output-port ) procedure
(current-error-port ) procedure
Returns the current default input port, output port, or error port (an output port), respectively.

(open-input-file string ) procedure
(open-binary-input-file string ) file library procedure
Takes a string for an existing file and returns a textual input port or binary input port that is capable of delivering data from the file. If the file does not exist or cannot be opened, an error that satisfies file-error? is signaled.

(open-output-file string ) procedure
(open-binary-output-file string ) file library procedure
Takes a string naming an output file to be created and returns a textual output port or binary output port that is capable of writing data to a new file by that name. If a file with the given name already exists, the effect is unspecified. If the file cannot be opened, an error that satisfies file-error? is signaled.

(close-port port ) procedure
(close-input-port port ) procedure
(close-output-port port ) procedure
Closes the resource associated with port, rendering the port incapable of delivering or accepting data. It is an error to apply the last two procedures to a port which is not an input or output port, respectively. Scheme implementations may provide ports which are simultaneously input and output ports, such as sockets; the close-input-port and close-output-port procedures can then be used to close the input and output sides of the port independently.

These routines have no effect if the port has already been closed.

(open-input-string obj ) procedure
Takes a string and returns a textual input port that delivers characters from the string. If the string is modified, the effect is unspecified.

(open-output-string ) procedure
Returns a textual output port that will accumulate characters for retrieval by get-output-string.

(get-output-string port ) procedure
It is an error if port was not created with open-output-string.

Returns a string consisting of the characters that have been output to the port so far in the order they were output. If the result string is modified, the effect is unspecified.

(open-input-bytevector bytevector ) procedure
Takes a bytevector and returns a binary input port that delivers bytes from the bytevector.

(open-output-bytevector ) procedure
Returns a binary output port that will accumulate bytes for retrieval by get-output-bytevector.

(get-output-bytevector port ) procedure
It is an error if port was not created with open-output-bytevector.

Returns a bytevector consisting of the bytes that have been output to the port so far in the order they were output.

husk-scheme online documentation rev 3.2 (2021.03.04)