1 / 10

16.216 ECE Application Programming

16.216 ECE Application Programming. Instructor: Dr. Michael Geiger Spring 2012 Lecture 34: Unformatted I/O. Lecture outline. Announcements/reminders Program 8 to be posted by tomorrow at the latest, due Friday 5/4 Program 9 will be extra credit only Posted by Monday 4/30 Due Monday 5/7

peri
Download Presentation

16.216 ECE Application Programming

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. 16.216ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 34: Unformatted I/O

  2. Lecture outline • Announcements/reminders • Program 8 to be posted by tomorrow at the latest, due Friday 5/4 • Program 9 will be extra credit only • Posted by Monday 4/30 • Due Monday 5/7 • No regrades allowed • If submitted, will replace your lowest grade • No lecture Friday • Today • Review: File I/O • Unformatted I/O ECE Application Programming: Lecture 34

  3. Review: File I/O • Open file: FILE *fopen(filename, file_access) • Close file: fclose(file_handle) • Formatted I/O: • fprintf(file_handle, format_specifier, 0+ variables) • fscanf(file_handle, format_specifier, 0+ variables) ECE Application Programming: Lecture 34

  4. File i/o function calls: unformatted I/O size_tfwrite(pointer, element size, # elements, file_handle) size_tfread(pointer, element size, # elements, file_handle) • pointer: address of data to be read/written • Typically an array, although can be scalar • element size: Size of each element in array • # elements: Number of elements in array • file_handle: is address returned by fopen() • Returns # of elements actually read/written • If < # elements requested, either error or EOF ECE Application Programming: Lecture 34

  5. Unformatted I/O (cont.) • Benefit—ability to read/write entire array or structure at once • For example: • Given int x[100]; • Can read array from file pointed to by fp: • n = fread(x, sizeof(int), 100, fp); • n should equal 100 • Can write array to file pointed to by fp: • fwrite(x, sizeof(int), 100, fp); ECE Application Programming: Lecture 34

  6. Character I/O • Output functions: send single character to output stream • intfputc(int c, FILE *stream); • intputc(int c, FILE *stream); • intputchar(int c); • Macro: #define putchar(c) putc((c), stdout) • Input functions • Read single character from input • intfgetc(FILE *stream); • intgetc(FILE *stream); • intgetchar(); • Macro: #define getchar() getc(stdin) • Return last character to input • intungetc(int c, FILE *stream); ECE Application Programming: Lecture 34

  7. Common uses • Read input character-by-character until EOF • while ((ch = getc(fp)) != EOF) { … } • Read character until it does not match format • Example: read digits until first non-digit encountered • while (isdigit(ch = getc(fp))) { … } ungetc(ch, fp); ECE Application Programming: Lecture 34

  8. Line I/O • Output functions • Write string + newline to stdout: int puts(const char *s); • Write string (no guaranteed newline) to stream: intfputs(const char *s, FILE *stream); • Input functions • Read line from stdin, up to first newline: char *gets(char *s); • Read line from stream: char *fgets(char *s, int n, FILE *stream); • fgets() can limit # characters read • Will read newline ECE Application Programming: Lecture 34

  9. End of file/error • Two ways to check for end of file: • Formatted I/O: Check if fscanf() == EOF • More common: do fscanf() as part of loop condition, and continue while EOF not reached • e.g. while (fscanf(fp, “%d”, &y) != EOF) • Unformatted: feof(file_handle); • Note: both functions indicate EOF after failed read operation • Must try to read data and discover that there’s nothing to read before testing for EOF • Checking for error (unformatted only): ferror(file_handle); ECE Application Programming: Lecture 34

  10. Next time (Monday, 4/30) • Unformatted I/O examples • Structures ECE Application Programming: Lecture 34

More Related