1 / 14

Section: Pset4

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

royce
Télécharger la présentation

Section: Pset4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS50 Section: Pset4 Jordan Jozwiak

  2. 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/

  3. This Week • Pipes & Redirecting • GDB debugging • File I/O

  4. 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

  5. GDB Debugging: get it?

  6. 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

  7. 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

  8. 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!

  9. 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

  10. 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.

  11. 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.

  12. File I/O: useful for pset4 unsigned char buffer[SIZE]; while (fread(buffer, 1, SIZE, file) == SIZE)    // do something with buffer

  13. Coding time!!! • cat.c • cp.c

  14. On your way out… • Go to http://cloud.cs50.net/~jjozwiak/to download this PowerPoint and a practice quiz to help review even more.

More Related