CSC151 2007S, Class 33: File Basics * I will continue to reserve time at the start of class for discussion of campus issues. Admin: * Happy April Fool's Day! * I distributed notes on exam 2 via email. Please let me know if you did not get those notes. * Are there questions on Assignment 7? * Reading for tomorrow: Pixel Maps. Overview: * File basics: Data persistence and beyond. * Ports. * Basic file operations. ==Focus: Files== * What is a file? * Something saved on your computer * "Permanently" stored (at least until your hard drive dies) * And that lots of programs can access * Simple distinction of files: * Text files - Contain lots and lots of characters; easy to read * Everything else * Since files are useful, we should be able to process them in Scheme * To process files, we need * Internal representation * Basic procedures * Standard Scheme techniques * Can we just refer to files by name? (read-from-file "/home/rebelsky/sample-file.txt") (read-from-file "/home/rebelsky/sample-file.txt") * Difficult to decide when read-from-file reads the *next* value and when it reads *the first* value * Scheme's solution: ports * Link to a named file * PLUS a position in the file * (read port) reads STARTING AT THE POSITION, and advances * When you read beyond the last value, read needs to say "Wait, there's nothing left" * Crash and burn "Error: Attempted to read beyond the end of a file." Or worse, "Success" * Return arbitrary value [BAD] * Return special "Sorry, you've hit the end of the file" value [Scheme's choice] You can also read characters, rather than whole values read-char * Also returns the same special value for end of file Also peek-char, which reads, BUT DOES NOT ADVANCE THE POSITION