Laboratory: Files
Summary:
In this laboratory, we explore file creation, input, and output
in Scheme.
Preparation
a. Scan through this lab to determine what kinds of tasks you'll need
to complete.
b. You should also scan through the reading on files in Scheme.
c. Open the reference on
files in Scheme in a separate window.
d. Start the GIMP and MediaScript.
e. Add the procedures from the reading (reprinted at the end of this
lab) to your definitions window.
Exercises
Exercise 1: Reading Values
As you may recall from the reading, we have prepared two simple files
for simple exploration of input and output,
/home/rebelsky/glimmer/samples/hi.txt and
/home/rebelsky/glimmer/samples/sample.txt.
a. Using a sequence of commands in the interaction window,
read all the characters from hi.txt. For example,
> (define source
(open-input-file "/home/rebelsky/glimmer/samples/hi.txt"))
> (read-char source)
#\H
...
> (read-char source)
&eof;
> (close-input-port source)
b. Using a sequence of commands in the interaction window,
read all the characters from sample.txt.
c. Using a sequence of commands in the interaction window,
read all the values from sample.txt (use
read rather than read-char).
d. Using a sequence of commands in the interaction window,
read all the values from hi.txt.
Exercise 2: Reading from Files, Revisited
The file /home/rebelsky/glimmer/samples/scheme-values.txt
contains the following:
23
A
"A"
#\A
(1 2 3)
a. Use read-char to confirm that it has that form.
> (define inport (open-input-file "/home/rebelsky/glimmer/samples/scheme-values.txt"))
> (read-char inport)
#\2
> (read-char inport)
#\3
> (read-char inport)
#\newline
> (read-char inport)
#\A
...
> (close-input-port inport)
b. What values do you expect repeated calls to read
to extract from that file?
c. Check your answer experimentally.
> (define inport (open-input-file "/home/rebelsky/glimmer/samples/scheme-values.txt"))
> (read inport)
...
> (close-input-port inport)
d. What type (e.g., number, list, string, etc.) does each value extracted
from that file have?
Exercise 3: Summing Values
The file /home/rebelsky/glimmer/samples/numbers.txt
contains five hundred and twenty-eight natural numbers.
a. Use sum-of-file from the reading to determine their sum.
b. How would you quickly determine if your attempt to sum those
numbers was correct?
Citation:
That file was copied from a similar file produced by Mr. Stone.
Exercise 4: File Length
Using sum-of-file (and its helpers) as a
pattern, write a Scheme procedure, (file-size
"file-name") that takes as argument a string that names a file
and returns the number of characters in that file (that is, the number
of times that read-char can be called to read a
character from the file without returning the end-of-file object).
Exercise 5: Missing Files
Find out what happens if sum-of-file or
file-size is given a string that does not name
any existing file.
Exercise 6: Creating Files
In the interactions pane, write a series of expressions that will
create a file, my-info, with the following lines
(substituting your own name and major).
Name: _last_, _first_
Major: _major-or-undeclared_
Exercise 7: Reusing Output Files
The Scheme standard says that if you try to open an output port to a
file that already exists, the effect is unspecified
, i.e.,
anything might happen. Hence, designers of a particular implementation
of Scheme are free to do what they choose.
Find out through experimentation what MediaScript does in this situation.
Exercise 8: Writing to Files, Revisited
Write a Scheme procedure, (dump-info file-name last-name
first-name major) that, given four strings as parameters,
writes the following to the file named by file-name,
Name: last-name, first-name
Major: major