CSC151.02, Class 33: Files Admin: * Are there questions on the exam? * Happy Halloween * No reading for Monday Overview: * Reflection on input and output. * Files: Semi-permanent storage. * Ports: The interface to files. * Example * Lab Look, it's ASCII Art ; +---+---+ +---+---+ ; | b | *-----> | a | / | ; +---+---+ +---+---+ Reflection on Input and Output * What input and output procedures do you know? * read * read-char * write-char * write * display * newline * How do write and display differ? * Write ties your shoe, display doesn't. (that was a joke) * write displays "exactly" (using Scheme syntax) * display writes "inexactly" (the way humans like) * Use write when you're creating output for Scheme to read later. * Use display when you're creating output for people to read. * Why does Scheme provide these procedures? * Makes it easier to interact with the Scheme program in a "natural" way. * Today we learn another reason: Input and output procedures can be used to store information. Observation: "Good" computer programs store information between executions of the program. * Scheme must therefore provide mechanisms for doing so. * Typical mechanism for storing information: The "file" * What does "file" mean in computerese? + Something you can open using a computer program? + Something that stores information on the computer. * What does "file" mean independent of computers? + A collection of information available for ready reference + Hmmm ... for once the computer folks chose a good name * Scheme provides what is typically called "Sequental" access to a file: You can only read or write the file a piece at a time * You know the operations for reading and writing files. * The only new one is read-char Scheme does not give you direct access to files for reading and writing. Instead, it makes you create "ports" to access to files. You need to name them * With define (define myport (open-input-file "my-important-information")) * With let (let ((myport (open-input-file "my-important-information")))) * By making them parameters to functions (define dosomething (lambda (myport) ...)) (dosomething (open-input-file "my-important-information")) Once you've named your ports, you can provide them as params to all the input and output functions. (read myport) -> Reads the "next" thing from the associated file and returns it. (read-char myport) -> Reads the next character fromt he associated file and returns it. (write myport) (display myport) ... * Ports permit multiple access to the same file. * Ports keep track of file position * You can use both read and read-char on the same file * Sam forgets hypens Why use read-char rather than read? * read-char is good for "plain text" * read is intended for Scheme values YOU CAN WRITE PROCEDURES THAT WRITE NEW PROCEDUERS AND TEACH SCHEME ABOUT THEM Try the lab