(import (rnrs)) ;R6RS (import (rnrs io simple)) ;R6RS (import (rnrs io ports)) ;R6RS (import (scheme file)) ;R7RS (import (scheme read)) ;R7RS (import (scheme write)) ;R7RS
Standards before R6RS did not specify the encoding of characters. There are no binary I/O ports; everything goes through the ports as characters in an unspecified encoding, which is commonly ISO-8859-1, but that could be anything, even some variant of EBCDIC.
There is nothing to help the programmer write a portable program to manipulate binary files. In practice, programs that need to work with binary data assume the implementation uses a single byte encoding scheme with a one-to-one correspondence between character numbers and bytes.
The process ports can be fetched by calling current-input-port(3) and current-output-port(3). Implementations can optionally provide with-input-from-file(3) and with-output-to-file(3), which dynamically rebind the input and output ports while a procedure is running. The procedures open-input-file(3) and open-output-file(3) open files for reading and for writing. They should be closed with close-input-port(3) and close-output-port(3) when the program is done with them, or there could be a resource leak.
The (rnrs io ports) library distinguishes between input ports and output ports. An input port is a source for data, whereas an output port is a sink for data. A port may be both an input port and an output port; such a port typically provides simultaneous read and write access to a file or other data.
The (rnrs io ports) library also distinguishes between binary ports, which are sources or sinks for uninterpreted bytes, and textual ports, which are sources or sinks for characters and strings.
Files can be opened using a specified transcoder or binary ports can be turned into textual ports by using a transcoder. Transcoders specify a character encoding, an end of line convention and an error handling mode. ISO-8859-1, UTF-8 and UTF-16 are supported along with all Unicode characters. It is straightforward for portable code to work with binary files and textual files with a known encoding like UTF-8.
Custom binary and textual ports can be used to implement things like network ports, transparent compression, other encodings and arbitrary processing. Custom ports are created with make-custom-binary-input-port(3), make-custom-textual-input-port(3), make-custom-binary-output-port(3), make-custom-textual-output-port(3), make-custom-binary-input/output-port(3) and make-custom-textual-input/output-port(3).
Portable R7RS code that attempts to use ports should be aware of these possible restrictions:
UTF-8 files can be read and written using binary ports together with utf8->string and string->utf8. It is an error to give invalid UTF-8 bytes to utf8->string, so this must be checked first.
In the 1990s it was still very common to have files in various local encodings, but Unicode has since then grown to become standard. Textual files created since the 2010s are usually encoded in UTF-8. A notable exception is files that come from Microsoft's Windows, which are sometimes encoded in UTF-16. But this is also becoming more rare with newer versions of that system.