CSC151 2009F, Class 53: File Basics Admin: * Reminder: Attendance is expected this week! * Tomorrow: What is CS? Revisited * Wednesday: What did we learn this semester * Friday: Evaluate the class. Thoughts on final. Final thoughts. Return exam. Tenative grades. * EC for * Marriage Equality Forum, Tonight at 7pm in JRC 101 * EC checklist distributed via email. * Let me know if I missed anything. * Get it to me soon! * I will reserve time at the start of class for questions on the exam. Overview: * Exam Questions. * File basics: Data persistence and beyond. * Ports. * Basic file operations. * Lab. Exam Questions * How do I swap the elements in a vector at positions x and y * This won't work (vector-set! vec x (vector-ref vec y)) (vector-set! vec y (vector-ref vec x)) * Why not? Think about it. * So, we need to remember the values (let ((valx (vector-ref vec x)) (valy (vector-ref vec y))) ...) * Can I build a new vector when reversing? * No. Your goal is to reverse in place. * Can you give me a hint as to how to write lookup-in-dictionary using binary-search (define lookup-in-dictionary (lambda (word dictionary) (let ((result (binary-search _____))) ...))) Today's topic: Files * A file is a collection of data, stored somewhere on the computer. * Files persist between invocations of a program. * Files can be shared between multiple programs. * Most languages include a way to work with files. * We can think of files two ways: * Just the name (a string) "/home/rebelsky/Desktop/stalkernet.jpg" * As a data type that we're working with * Also has a sense of where in the file we are. (cursor) * A file plus a cursor is a "port" Operations: * From file name to port (open-input-file FILENAME) returns a port (open-output-file FILENAME) returns a port * Indicate that we're done (close-input-port PORT) (close-output-port PORT) * Reading from input ports (read-char PORT) (read PORT) * Writing to output ports (display VALUE PORT) (newline PORT) (write PORT) (write-char PORT) > (display "Wake up Sam!" PORT) Puts Wake up Sam! into the output file > (write "Wake up Sam!" PORT) Puts "Wake up Sam!" into the output file > (write (cool-proc param1) PORT) Writes the result of calling cool-proc on param1