Arc has a large variety of I/O operations that support file, string, and network I/O. Because Arc's I/O system is based on MzScheme, the MzScheme I/O documentation provides useful background.
Arc's I/O system is built in layers. The foundation provides basic operations. On top of this, more functional operations are constructed. The w/
(with/) series of operations are generally the most convenient.
One main I/O abstraction in Arc is the port: an input port produces bytes and an output port consumes bytes. A port may be created by opening a file or socket. In addition, Arc supports string I/O, where a string can provide data as an input port, or can receive data as an output port.
For input, Arc provides support to read a byte, character, line, or Scheme S-expression. (Because Arc inherit's MzScheme's Unicode support, one Unicode character may involve more than one byte.)
Some of Arc's input operations indicate end-of-file through a user-specified eof value. This can be a symbol such as 'eof
or nil
.
Arc's read methods take a variety of arguments: some require a port, some default to stdin
, and some allow a string as input. Some take an arbitrary number of body statements, while some take a "thunk", a function to evaluate.
Arc provides multiple operations for output. The prn
procedure is useful for general-purpose printing, as well as for debugging. By wrapping it with w/stdout
, the output can be directed to a file or string.
Arc supports formatted output, with very limited control over formatting. The prf
operation uses a format string, which can contain two types of formatting directives. A "#" followed by a form causes the form to be evaluated. A "~" followed by an ignored character causes the directive to be replaced by the next argument. For example:
arc> (let x 42 (prf "#(+ 1 1) ~x #x\n" 111)) 2 111 42 ""
One convenient pattern in Arc is to read and write S-expressions to avoid encoding and parsing data. Multiple operations support reading S-expressions, either individually or an entire set at once, from multiple sources. If the syntactic correctness of the input is uncertain, saferead
can be used.
Arc also includes multiple string I/O operations that allow a string to be used for input or output. The simplest operations are fromstring
and tostring
, for input or output respectively. See MzScheme String Ports for background on String Port I/O.