Class 45: Files in Scheme
Held:
We consider *files, a technique for structuring information that permits the information to persist across invocations of Scheme. Files also let our Scheme programs share information with other programs.*
Preliminaries
Overview
- File basics
- Data persistence
- Ports
- Basic file operations
Related Pages
-
Reading: Files Reading
-
Lab: Files in Scheme
Updates
News / Etc.
- Sit with project partners!
- Welcome to any prospectives who are here.
- We’ll try to split class between lab work and project work.
Upcoming Work
- Lab writeup: Exercise 4 (it’s class 45)
- Title your email CSC 151.01 Writeup for Class 45 (YOUR FULL NAMES).
- Reading for Tuesday: Analyzing Procedures
- Project Proposals due TONIGHT (sketches due Tuesday).
- Make sure to describe the underlying design goals.
- Make sure to indicate the three techniques you will use.
- Make sure to explain how you know you will get 1000 different images.
Extra credit (Academic/Artistic)
- CS Table Tuesday, noon: Algorithmic [sic] bias
- CS Extras Thursday, 4:15pm: Gadfly
Extra credit (Peer)
- AAA hosts Kit Yan performing Queer Heartache, Wednesday, Harris, 7pm.
- Next home baseball game: May 7 (Senior Day)
Extra credit (Misc)
- May 4 town hall on belonging. 11am, May 4, JRC 101
Other good things to do
- Dag Field Day, Saturday, April 29, noon-5pm, in the Club Athletic Field.
Why Use Files?
- As I hope you’ve figured out by now, it is possible (although not necessarily easy) to use Scheme to do “anything” you can do on the computer.
- Two similar things that you often want to do are to save data to files and to recover data from files.
- Why?
- So that data can last a long time.
- So that you can deal with more data than you can easily enter by hand.
- So that you can write a word processor.
- …
- As you might guess, you can do both activites with Scheme.
Ports
- Rather than dealing directly with files, Scheme adds a layer of abstraction called a port.
- Each port is associated with something that can be used for input
or output.
- That thing can be a file.
- That thing can also be the keyboard (for input), the screen (for output), or a network connection.
- Why do we have ports?
- So that the process of writing anywhere (or reading anywhere) is the same; our code doesn’t need to change.
- So that we can read from the same file more than once simultaneously and not get lost about where we are in the file.
- In effect, an input port has two parts:
- a link to the file that it is associated
- a cursor that indicates where in the file the next read should take place.
- To create a port that corresponds to a file that you want to read from,
use
(open-input-file *file-name*). - To create a port that corresponds to a file that you want to write to,
use
(open-output-file *file-name*). - You can read from input ports with
(read *port*) - You can write to ports with
(newline *port*)(write *value* *port*)(display *value* *port*)
- When you’re done with an input port, use
(close-input-port *port*) - When you’re done with an output port, use
(close-output-port *port*) - What does
readdo when there’s nothing left in the file? It returns a special value (which DrScheme displays as#<eof>). - You can tell that that value indicates the end of the file with
eof-object?
Characters
- You can read a character at a time (rather than a value at a time) using
(read-char *port*). - Why is this useful? It lets you be more general than the
basic
readprocedure. - You can also look at the next character that you’re about to read
using
(peek-char *port*) - Why is this useful? Sometimes you want to read differently based on
what you see next.
- If you see a semicolon, read the whole line as a string and ignore it.
- If you see anything else, read it as a Scheme value
- When
read-charencounters the end of the file, it returns the same special value asread
Lab
- Do the lab on files.