Download presentation
Presentation is loading. Please wait.
1
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Fall 2018 Lecture 33: File I/O (continued)
2
ECE Application Programming: Lecture 33
Lecture outline Announcements/reminders Office hours Tuesday 1:30-3, not Thursday this week W 12/5: Program 7 due M 12/10: Program 6 regrades due Th 12/13: last day of classes; Program 8 due P8 deals with file I/O (lectures 32-33) M 12/17: Exam 3, 3-6 PM, Ball 210 Will post course evals online; you’ll submit eval at exam W 12/19: All code due by 12:00 PM (noon) Program 9: Worth up to 4 points extra credit on final avg Resubmission deadline for P7 & P8 Today’s class Review: file I/O Character & line I/O 5/23/2019 ECE Application Programming: Lecture 33
3
ECE Application Programming: Lecture 33
Review: File I/O Open file: FILE *fopen(filename, file_access) Returns NULL if file can’t be opened Close file: fclose(file_handle) Formatted I/O: fprintf(file_handle, format_specifier, 0+ variables) fscanf(file_handle, format_specifier, 0+ variables) 5/23/2019 ECE Application Programming: Lecture 33
4
ECE Application Programming: Lecture 33
Review: Generic I/O Three special I/O streams in C stdin: standard input stdout: standard output stderr: standard error stream printf(“Hello\n”) == fprintf(stdout, “Hello\n”); scanf(“%d”, &x) == fscanf(stdin, “%d”, &x); Can write generic functions that deal either with specific file or standard input/output 5/23/2019 ECE Application Programming: Lecture 33
5
ECE Application Programming: Lecture 33
Example: File I/O Write a program to: Read three integer values from the file myinput.txt Determine sum and average Write the original three values as well as the sum and average to the file myoutput.txt Note that: The program should exit if an error occurs in opening a file 5/23/2019 ECE Application Programming: Lecture 33
6
ECE Application Programming: Lecture 33
The program (part 1) #include <stdio.h> int main() { int v1, v2, v3, sum; // Input values and sum double avg; // Average of x, y, and z FILE *fpIn, *fpOut; // File pointers // Open input file, exit if error fpIn = fopen("myinput.txt","r"); if (fpIn == NULL) { printf("Error opening myinput.txt\n"); return 0; } // Can actually open file as part of conditional statement if ((fpOut = fopen("myoutput.txt","w")) == NULL) { printf("Error opening myoutput.txt\n"); 5/23/2019 ECE Application Programming: Lecture 33
7
ECE Application Programming: Lecture 33
The program (part 2) // Read the three values fscanf(fpIn, "%d %d %d", &v1, &v2, &v3); // Compute sum and average sum = v1 + v2 + v3; avg = sum / 3.0; // print out values fprintf(fpOut, "Values: %d, %d, %d\n", v1, v2, v3); fprintf(fpOut, "Sum: %d\n",sum); fprintf(fpOut, "Avg: %lf\n",avg); // close the files fclose(fpIn); fclose(fpOut); return 0; } 5/23/2019 ECE Application Programming: Lecture 33
8
File i/o function calls: unformatted I/O
size_t fwrite(pointer, element size, # elements, file_handle) size_t fread(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 5/23/2019 ECE Application Programming: Lecture 33
9
Unformatted I/O (cont.)
One benefit—ability to read/write entire array 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); 5/23/2019 ECE Application Programming: Lecture 33
10
ECE Application Programming: Lecture 33
End of file/error Two ways to check for end of file: Text files: 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) Binary files: 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 (binary only): ferror(file_handle); 5/23/2019 ECE Application Programming: Lecture 33
11
ECE Application Programming: Lecture 33
Next time Character & line I/O Program 8 preview Reminders: Office hours Tuesday 1:30-3, not Thursday this week W 12/5: Program 7 due M 12/10: Program 6 regrades due Th 12/13: last day of classes; Program 8 due P8 deals with file I/O (lectures 32-33) M 12/17: Exam 3, 3-6 PM (room TBD) Will post course evals online; you’ll submit eval at exam W 12/19: All code due by 12:00 PM (noon) Program 9: Worth up to 4 points extra credit on final avg Resubmission deadline for P7 & P8 5/23/2019 ECE Application Programming: Lecture 33
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.