Lab: Files

Wednesday, Nov 7, 2018
Lab writeups are due at 10:30pm on the day of the next class meeting. For example, a Wednesday lab is due at 10:30pm on Friday. Each lab writeup will be announced at the end of class on a lab day.
In this laboratory, we explore file creation, file input, and file output in Scheme.


a. Skim through this lab to determine what kinds of tasks you’ll need to complete.

b. You should also review the reading on files in Scheme.

c. Make a copy of the code from the reading.


Exercise 1: 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 2: 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).

Note that you should not use read in your procedure.

Exercise 3: 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 4: Creating files

a. In the interactions pane, write a series of expressions that will create a file, my-info, with three lines, each of the form

  • last name
  • a comma
  • first name
  • a comma
  • major
  • a comma
  • year

For example, the first line might read

Rebelsky,Samuel,Film Studies,2017

b. Determine what happens when you read that file back using file->lines.

c. Determine what happens when you read that file back using read-csv-file.

Exercise 5: 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”. That is, anything might happen. Hence, designers of a particular implementation of Scheme are free to do what they choose.

a. Find out through experimentation what DrRacket does in this situation.

b. Search the Web for the Racket documentation on open-output-file and find out how to overwrite an existing file.

Exercise 6: Writing to files, revisited

a. Write a Scheme procedure, (student-info output-port last-name first-name major year) that writes one line in the form specified above.

b. Write a Scheme procedure (dump-info file-name list-of-data) that, given a list of student data, with each entry of the form (last first major year), writes all the data to the given file.

(dump-info "/home/username/Desktop/data.csv"
           (list (list "Rebelsky" "Samuel" "Film Studies" 2017)
                 (list "Dent" "Stu" "Liberal Arts" 2021)
                 (list "K" "Russell" "Law" 1950)
                 (list "Again" "Mash" "Robotic Soccer" 2020)))

c. Confirm that your procedure works correctly by opening the file in DrRacket.

d. Confirm that your procedure works correctly by opening the file using read-csv-file.

For those with extra time

Extra 1: Displaying files

Write a procedure, (display-file filename), that takes the name of a file as a parameter and displays the contents of the file with each line preceded by its line number.

Please read directly from the file. You may not use file->lines, file->words, or file->chars in this definition.

Extra 2: Finding divisors

Use the store-divisors procedure from the reading to draw up a list of the divisors of 120, storing them in a file named divisors-of-120. Examine the file afterwards and confirm that the answer is correct.

By the way, don’t give this procedure an extremely large number as argument – it’s much too slow. There are more efficient ways to find divisors!

Extra 3: Counting vowels

Write a Scheme procedure that takes as arguments two file names (an input file and an output file), counts the number of occurrences of each vowel in the input file, and writes the result to the output file. Note that the output file should have the following form (with numbers in place of the number signs):