yen
Uploaded by
16 SLIDES
292 VUES
160LIKES

Understanding File I/O and Binary Trees in CS222: Key Takeaways from Week 11

DESCRIPTION

In Week 11 of CS222, we explored essential concepts around binary trees and file I/O operations. Key highlights include character by character output using functions like `fputc()` and `putc()`, as well as input functions like `fgetc()` and `getc()`. We discussed error handling, particularly when files cannot be opened, and covered standard streams such as `stdin`, `stdout`, and `stderr`. Additionally, we reviewed project progress and upcoming topics, including binary files, with reinforced reminders to continue working on Project 5 and study chapters 4 and 5 from the LPI.

1 / 16

Télécharger la présentation

Understanding File I/O and Binary Trees in CS222: Key Takeaways from Week 11

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

Playing audio...

  1. Week 11 - Friday CS222

  2. Last time • What did we talk about last time? • Binary trees • File I/O

  3. Questions?

  4. Project 5

  5. Quiz

  6. Quotes The key to performance is elegance, not battalions of special cases. The terrible temptation to tweak should be resisted unless the payoff is really noticeable. Jon Bently and M. Douglas McIlroy Computer Scientists at Bell Labs

  7. File I/O

  8. fputc() and putc() • If you need to do character by character output, you can use fputc() • The first argument is the file pointer • The second is the character to output • putc() is an equivalent function FILE* file = fopen("output.dat", "w"); inti = 0; for(i = 0; i < 100; i++ ) fputc(file, '$');

  9. fgetc() and getc() • If you need to do character by character input, you can use fgetc() • The argument is the file pointer • It returns the character value or EOF if there's nothing left in the file • getc() is an equivalent function FILE* file = fopen("input.dat", "r"); int count = 0; while(fgetc(file) != EOF ) count++; printf("There are %d characters in the file\n", count);

  10. Error handling • Lots of errors can happen with file I/O • If a file cannot be opened with the given mode, fopen() returns NULL and errno is set to an appropriate error code • The fprintf() function returns the number of characters written • A value less than or equal to 0 indicates error • The fscanf() function returns the number of items read • If that number is less than expected, it's an error

  11. Standard streams • C programs that run on the command line have the following file pointers open by default • stdin • stdout • stderr • You can use them where you would use other file pointers

  12. Aliases for other functions • You can think of the input and output functions you've been using as special cases of these file operations • They are often implemented that way • For example: • getchar() is equivalent to fgetc(stdin) • printf(…) is equivalent to fprintf(stdout,…) • scanf(…) is equivalent to fscanf(stdin, …)

  13. Lab 11

  14. Upcoming

  15. Next time… • Binary files • More file examples • File I/O, the universal model

  16. Reminders • Keep working on Project 5 • Read LPI chapters 4 and 5

More Related