Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECE.2160 ECE Application Programming

Similar presentations


Presentation on theme: "EECE.2160 ECE Application Programming"— Presentation transcript:

1 EECE.2160 ECE Application Programming
Instructors: Dr. Lin Li & Dr. Michael Geiger Spring 2019 Lecture 32: File I/O

2 ECE Application Programming: Lecture 32
Lecture outline Announcements/reminders Program 7 due today (4/24) Program 8 due 5/3 Course evaluations to be posted online; returned at final exam Today’s class Program 8 intro File I/O 7/24/2019 ECE Application Programming: Lecture 32

3 ECE Application Programming: Lecture 32
File information Files commonly used for input/output Interface uses FILE pointers FILE type defined in <stdio.h> Allows access to necessary file characteristics File characteristics include: Name—for example z:\Visual Studio 2010\Projects\fileio\fileio\myinput.txt Read/write permission Type (binary or ASCII text) Access (security; single/multiple user) Position in file Programmer doesn’t have to account for this info 7/24/2019 ECE Application Programming: Lecture 32

4 Basic file I/O functions
Before reading/writing file, program must gain access to file fopen() function used to open file Returns FILE * if successful, NULL otherwise When done with file, program should close it fclose() function used to close file 7/24/2019 ECE Application Programming: Lecture 32

5 ECE Application Programming: Lecture 32
fopen() FILE *fopen(char *fname, char *faccess) fname: name of file (e.g., "f1.txt") Name may require full path faccess: string providing First char: access mode r/w/a (read/write/append) Write starts at beginning of file, append starts at end Either write or append creates new file if none exists Additional (optional) char: file type b/t (binary/text) Text files are human readable (default--don’t need t) Binary files are just raw bytes Valid access strings: "r", "w", "a", "rb", "wb", "ab" 7/24/2019 ECE Application Programming: Lecture 32

6 ECE Application Programming: Lecture 32
fopen() (continued) If successful, fopen() returns valid FILE * to be used with file read/write functions If unsuccessful, fopen() returns NULL Test this error condition to ensure file opened Why might fopen() be unsuccessful? Try to open input file that doesn’t exist Try to open read-only file for writing Try to open file that’s locked by another application 7/24/2019 ECE Application Programming: Lecture 32

7 ECE Application Programming: Lecture 32
fopen() example FILE *fp; fp = fopen("in1.txt", "r"); if (fp == NULL) printf("Couldn’t open file\n"); else { … // FILE * is valid // Continue with program } 7/24/2019 ECE Application Programming: Lecture 32

8 File i/o function calls
fclose(FILE *file_handle) Closes a file Argument is address returned by fopen() fclose(fp); Recommended for input files Required for output files OS often doesn’t write last bit of file to disk until file is closed 7/24/2019 ECE Application Programming: Lecture 32

9 Example of basic file function usage
int main() { FILE *fp; // Open text file for reading fp = fopen("in.txt", "r"); if (fp == NULL) { printf("Error: could not open in.txt"); return 0; } ... // CODE TO EXECUTE IF FILE OPENS fclose(fp); // Close file when done 7/24/2019 ECE Application Programming: Lecture 32

10 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 7/24/2019 ECE Application Programming: Lecture 33

11 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"); 7/24/2019 ECE Application Programming: Lecture 33

12 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; } 7/24/2019 ECE Application Programming: Lecture 33

13 File i/o function calls: formatted I/O
fprintf(file_handle, format_specifier, 0+ variables) file_handle: address returned by fopen() Other arguments are same as printf() Example: fprintf(fp, "x = %d", x); fscanf(file_handle, format_specifier, 0 or more variables) Other arguments are same as scanf() Example: fscanf(fp, "%d%d", &a, &b); 7/24/2019 ECE Application Programming: Lecture 32

14 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); 7/24/2019 ECE Application Programming: Lecture 33

15 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); 7/24/2019 ECE Application Programming: Lecture 33

16 ECE Application Programming: Lecture 32
Next time Character and line I/O Reminders: remaining key dates Program 7 due today (4/24) Program 8 due 5/3 Course evaluations to be posted online; returned at final exam 7/24/2019 ECE Application Programming: Lecture 32


Download ppt "EECE.2160 ECE Application Programming"

Similar presentations


Ads by Google