CSC151.01 2009F Functional Problem Solving : Reference

Files


Opening and Closing Files

(open-input-file filename)
Standard File Procedure. Open the specified file for reading. Returns an input port.
(open-output-file filename)
Standard File Procedure. Open the specified file for writing. Returns an input port.
(close-input-port input-port)
Standard File Procedure. Close an open input port. (It is an error to try to close something that is not an input port, or an input port that is already closed.)
(close-output-port output-port)
Standard File Procedure. Close an open output port. (It is an error to try to close something that is not an output port, or an output port that is already closed.)

Reading From Files

(read input-port)
Standard File Procedure. Read the next value available on the specified port. If no characters remain, returns the end-of-file object.
(read-char input-port)
Standard File Procedure. Read the next character available on the specified port. If no characters remain, returns the end-of-file object.
(peek-char input-port)
Standard File Procedure. Determine the next character available on the specified port. If no characters remain, returns the end-of-file object.

Writing To Files

(display value output-port)
Standard File Procedure. Print a human-readable representation of value on the specified port.
(write value output-port)
Standard File Procedure. Print the verbose representation of the specified value to the specified port.
(write-char ch output-port)
Standard File Procedure. Write the the given character to the specified port.
(newline output-port)
Standard File Procedure. Write a newline (carriage return) to the specified output port.

Predicates

(input-port? val)
Standard File Predicate. Determine if val is an open input port.
(output-port? val)
Standard File Predicate. Determine if val is an open output port.
(file-exists? filename)
Standard File Procedure. Determine whether a file with the given name exists.
(eof-object? val)
Standard File Procedure. Determine if val is something returned by read (or read-char or peek-char) to indicate the end of input.

Miscellaneous

(delete-file filename)
Common File Procedure. Delete the file specified by filename. If the file doesn't exist, reports an error.

Creative Commons License

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright (c) 2007-9 Janet Davis, Matthew Kluber, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials copyright by John David Stone and Henry Walker and used by permission.)

This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.

This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.