40 likes | 112 Views
Solving a problem: scandir and Unix ls. Access all the entries in a directory, or selected entries ls *.cc, ls -l *, ls -lt foo.* process the entries in some way (add sizes, print, …) What is in a directory? ., .., files, subdirectories
E N D
Solving a problem: scandir and Unix ls • Access all the entries in a directory, or selected entries • ls *.cc, ls -l *, ls -lt foo.* • process the entries in some way (add sizes, print, …) • What is in a directory? • ., .., files, subdirectories • what is a file in Unix? What kind of resources are associated with a file? How do you create/remove a file from within a running program? • How are files and directories accessed from a program? • How is a directory tree backed-up to disk/tape? • What are the issues?
Solving another problem: search for words • Web-based search engines • Search for “Titanic” + “Linux” in Alta vista • File-based search engines • What’s the file that has the reference to “elephants”? • What are the issues in the searches? • User’s perspective • Programmer’s perspective • What about approximate matches • search for “pincake” or “pancake” or “pancakes” or … • are approximate/fuzzy matches a good idea?
Coping with transferable ownership • Factory creates a class, who deletes it? Foo * f = FooFactory::makeFoo(); f->doFooStuff(); delete f; • Why might this be a problem? What are alternatives? • Instead of using a Foo pointer, use a proxy, a class that stands in for a Foo pointer • looks like a Foo pointer, acts like a Foo pointer, but cleans up after itself (SmartStrings in sortall/cps 100): heap vs. stack allocation FooProxy f(FooFactory::makeFoo()); f->doFooStuff();
Program parameters and errors • What are alternatives to large if/else chains to process argc and argv? • What are argc/argv used for? • What helper functions cope with arg parsing? • What’s wrong with if (argv[k] == “-f”) • Other options to getting info into program • How do you signal an error condition? • Files don’t open, times (wag) out of order, … • cerr << • other alternatives?