Next: , Previous: Pre-Scheme error handling, Up: Standard Pre-Scheme environment


9.3.6 Input & output

Pre-Scheme's I/O facilities are somewhat different from Scheme's, given the low level and the static type strictness. There is no exception mechanism in Pre-Scheme; everything is maintained by returning a status token, as in C. Pre-Scheme's built-in I/O facilities are buffered. 1 (see Low-level Pre-Scheme memory manipulation, for two other I/O primitives, read-block & write-block, for reading & writing blocks of direct memory.)

— procedure: open-input-file filename –> [port status]
— procedure: open-output-file filename –> [port status]
— procedure: close-input-port input-port –> status
— procedure: close-output-port output-port –> status

Open-input-file & open-output-file open ports for the given filenames. They each return two values: the newly open port and an errors enumerand status. Users of these procedures should always check the error status before proceeding to operate with the port. Close-input-port & close-output-port close their port arguments and return the errors enumerand status of the closing.

— procedure: read-char input-port –> [char eof? status]
— procedure: peek-char input-port –> [char eof? status]
— procedure: read-integer input-port –> [integer eof? status]

Read-char reads & consumes a single character from its input-port argument. Peek-char reads, but does not consume, a single character from input-port. Read-integer parses an integer literal, including sign. All of these also return two other values: whether or not the file is at the end and any errors enumerand status. If any error occurred, the first two values returned should be ignored. If status is (enum errors no-errors), users of these three procedures should then check eof?; it is true if input-port was at the end of the file with nothing more left to read and false otherwise. Finally, if both status is (enum errors no-errors) and eof? is false, the first value returned may be safely used.

— procedure: write-char char output-port –> status
— procedure: newline output-port –> status
— procedure: write-string string output-port –> status
— procedure: write-integer integer output-port –> status

These all write particular elements to their output-port arguments. Write-char writes individual characters. Newline writes newlines (line-feed, or ASCII codepoint 10, on Unix). Write-string writes the contents of string. Write-integer writes an ASCII representation of integer to port, suitable to be read by read-integer. These all return an errors enumerand status. If it is no-errors, the write succeeded.

— procedure: force-output output-port –> status

Forces all buffered output in output-port. Status tells whether or not the operation was successful.


Footnotes

[1] Scheme48's VM does not use Pre-Scheme's built-in I/O facilities to implement channels — it builds its own lower-level facilities that are still OS-independent, but, because they're written individually for different OSs, they integrate better as low-level I/O channels with the OS. On Unix, the Scheme48 VM uses file descriptors; Pre-Scheme's built-in I/O uses stdio. Scheme48's VM uses Pre-Scheme's built-in I/O only to read heap images.