CS153, Class 28: Files Overview * What is a file? * Ports, a key file abstraction. * Lab Notes: * Three hours sleep is not enough. Do not expect coherency. * Sam gone this weekend. * Today is our last day with Scheme (sniff). Starting next week, we'll spend a few weeks learning Java fundamentals and then discuss lots and lots of data structrues and algorithms. * Any questions on exam 2? Bring questions on Monday. Q: "Make a table" A: Tabs are fine. Do not use Word! ---------------------------------------- What is a file? * A stable set of data which can be accessed by other programs. * What does "stable" mean? + A place where horses live? + It doesn't go away unless someone tells it to. + In theory, it shouldn't change (unless you tell it to change) * Files are "normally" stored on a disk. * In modern programming tradition, we name files and that's how other programs can access them. * Most computer scientists say "Persistent" rather than "Stable" - meaning it persists even after your program goes away. What do you do with files? * Affect content + Read from them + Write to them * Affect which ones are avaiable + Rename + Create + Delete Sam's silly sample file 'Twas brillig and the slithy toves did gyre and gimble in the wabe. All mimsy were the borogoves and the mome raths outgrabe. What's the first thing I read from this file? * Standard/simple mechanism: Read characters (apostrophe) * Scheme mechanism: Read scheme values The list (quote twas) Suppose we're using the "read character" model. What happens if I read again? * Something related to the capital T. #\T * Reading (and writing) require more than a file. They also require a notion of current position. In Scheme, this abstraction and combination is called a "port" * You build ports from files and then read and write with ports. (open-input-file "filename") Returns an input-port (close-input-port input-port) Good practice to say you're done with a file (read input-port) (read-char input-port) (peek-char input-port) (open-output-file "filename") Returns an output-port (close-output-port output-port) Good practice to say you're done with a file (file-exists? "filename") (delete-file "filename")