140 likes | 307 Vues
CS50. Section: Pset4. Jordan Jozwiak. Announcements. Pset3 was returned on Tuesday night last week Pick up Quiz 0 at end of class! Our section was 4% above the mean! Check50 is now working Follow the instructions at https://www.cs50.net/. This Week. Pipes & Redirecting GDB debugging
E N D
CS50 Section: Pset4 Jordan Jozwiak
Announcements • Pset3 was returned on Tuesday night last week • Pick up Quiz 0 at end of class! • Our section was 4% above the mean! • Check50 is now working • Follow the instructions at https://www.cs50.net/
This Week • Pipes & Redirecting • GDB debugging • File I/O
Pipes & Redirecting • Tommy’s Short in Week 5 of https://www.cs50.net/shorts/ • To which “stream” do functions like printf write by default? • stdout • What’s the difference between > and >>? • overwrite, append • What pipeline could you use to find the number of unique names in a file called names.txt • cat names.txt | wc -l
GDB Debugging: getting started • Open the Appliance • cd ~/Dropbox • wgethttp://cdn.cs50.net/2012/fall/sections/5/section5.zip • unzip section5.zip • Begin debugging • Try running: ./buggy1 • Then debug: gdb ./buggy1 • gdb –tui ./buggy1
GDB Debugging: Useful commands • Useful commands • r(un) • n(ext) • q(uit) • bt(backtrace) • u(p) • d(own) • l(ist) • p(rint) <variable name> • display <variable name> • info locals • info globals • info args
File I/O • We are accustomed to reading from and writing to the terminal. • More formally, we read from stdin, write to stdout. • We can also read from and write to files!
File I/O: library functions • fopen – open a file • fclose – close a file • fread – read from a file • fwrite – write from a file • fseek - move to a particular point in a file
File I/O: usage • Must always open a file before reading from or writing to it. • Should always close a file whenever you open one! • fopen returns a FILE*, a pointer to a ‘file handle’. • We use the FILE* to access and close our file.
File I/O: things to avoid while (!feof(file)) • this isn't correct because the EOF flag is "set" *after* a read call (fgetc, fread, etc.) fails, not *before*. char* buffer = malloc(LOTS_OF_BYTES); while (fread(buffer, 1, LOTS_OF_BYTES, file)) • this isn't good because you should just use a stack array (of char or unsigned char) for your buffers. • Remember that if you're reading in strings (or plan on calling string functions on the buffer), you'll need to properly null-terminate (and save space for the '\0', which probably means adding an extra + 1 to your buffer)! for (char c = fgetc(file); c != EOF; c = fgetc(file)) • this is *almost* correct, but it fails because fgetc returns an int, not a char. You absolutely *must* use an int here.
File I/O: useful for pset4 unsigned char buffer[SIZE]; while (fread(buffer, 1, SIZE, file) == SIZE) // do something with buffer
Coding time!!! • cat.c • cp.c
On your way out… • Go to http://cloud.cs50.net/~jjozwiak/to download this PowerPoint and a practice quiz to help review even more.