Fundamentals of CS I (CS151 2001S) : Outlines
[Current]
[Discussions]
[Glance]
[Honesty]
[Instructions]
[Links]
[News]
[Search]
[Syllabus]
Primary
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Quizzes]
[Readings]
[Reference]
Sets
[Blackboard]
[Scheme Report]
[SamR's Schedule]
[Rebelsky/Fall 2000]
[Walker/Fall2000]
[Stone/Spring2000]
Links
Reading: Files. Lab: Files. Back to Program Input and Output. On to Discussion of Exam 1.
Held Monday, March 5, 2001
Summary
Today we consider how to make Scheme read values and characters from files and write values to other files.
Assignments
Notes
inputas a verb.
;;; Procedure:
;;; read-value
;;; Parameters:
;;; prompt, a string
;;; Purpose:
;;; Prints a request for information, reads a value, and returns
;;; the value read.
;;; Produces:
;;; val, a value. (Can be any type of value.)
;;; Preconditions:
;;; It is still possible to print output to the standard output port.
;;; It is still possible to read input from the standard input port.
;;; prompt is a string.
;;; Postconditions:
;;; Additional text may have appeared on output.
;;; A value will have been read from input.
;;; val is the value that was read from input.
(define read-value
(lambda (prompt)
(display prompt)
(read)))
;;; Procedure:
;;; read-number
;;; Parameters:
;;; prompt, a string
;;; Purpose:
;;; Print a request for information and repeatedly read a value
;;; until a number is entered.
;;; Produces:
;;; val, a number
;;; Preconditions:
;;; It is still possible to print output to the standard output port.
;;; It is still possible to read input from the standard input port.
;;; prompt is a string.
;;; Postconditions:
;;; Additional text will have appeared on output.
;;; One or more values will have been read from input.
;;; The first number entered after this procedure is called will
;;; be returned.
(define read-number
; Helper: Print an error message and try again
(let ((failure (lambda (prompt val)
(display "That wasn't a number!")
(newline)
(read-number prompt))))
(lambda (prompt)
(display prompt)
; Read a value
(let ((val (read)))
(if (number? val)
val
(failure prompt val))))))
Overview
anythingyou can do on the computer.
(open-input-file file-name).
(open-output-file file-name).
(read port)
(newline port)
(write value port)
(display value port)
(close-input-port port)
(close-output-port port)
(let* (; Prepare to read from a file
(source (open-input-file "data"))
; Read one value
(value (read source)))
; We're done, so clean up.
(close-input-port source)
; Return the value read
value)
read do when there's nothing left in the file?
It returns a special value (which DrScheme displays as
#<eof>).
eof-object?
(read-char port).
read procedure.
(peek-char port)
read-char encounters the end of the file, it returns
the same special value as read
Back to Program Input and Output. On to Discussion of Exam 1.
[Current]
[Discussions]
[Glance]
[Honesty]
[Instructions]
[Links]
[News]
[Search]
[Syllabus]
Primary
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Quizzes]
[Readings]
[Reference]
Sets
[Blackboard]
[Scheme Report]
[SamR's Schedule]
[Rebelsky/Fall 2000]
[Walker/Fall2000]
[Stone/Spring2000]
Links
Disclaimer: I usually create these pages on the fly. This means that they are rarely proofread and may contain bad grammar and incorrect details. It also means that I may update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.
This page was generated by Siteweaver on Wed May 5 12:14:59 2004.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2001S/outline.25.html.
You may validate
this page's HTML.
The source was last modified Tue Jan 23 16:01:58 2001.