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
Instructor: Dr. Michael Geiger Fall 2018 Lecture 32: Finish PE3 File I/O

2 ECE Application Programming: Lecture 32
Lecture outline Remaining key dates 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 Today’s class Finish PE4 (search functions) File I/O 6/30/2019 ECE Application Programming: Lecture 32

3 ECE Application Programming: Lecture 32
Review: PE3 Given header files, main program Complete specified functions For the Name structure void printName(Name *n): Print the name pointed to by n, using format <first> <middle>. <last> void readName(Name *n): Prompt for and read a first, middle, and last name, and store them in the structure pointed to by n SINew functions on next slide 6/30/2019 ECE Application Programming: Lecture 32

4 Review: PE3 (continued)
Given header files, main program Complete specified functions Name functions on previous slide For the SINew structure void printStudent(SINew *s): Print information about the student pointed to by s void readStudent(SINew *s): Prompt for and read information into the student pointed to by s void printList(SINew list[], int n): Print the contents of an array list that contains n StudentInfo structures int findByLName(SINew list[], int n, char lname[]): Search for the student with last name lname in the array list. Return the index of the structure containing that last name, or -1 if not found int findByID(SINew list[], int n, unsigned int sID): Search for the student with ID # sID in the array list. Return the index of the structure containing that last name, or -1 if not found 6/30/2019 ECE Application Programming: Lecture 32

5 ECE Application Programming: Lecture 32
Name functions void printName(Name *n) { printf("%s %c. %s\n", n->first, n->middle, n->last); } void readName(Name *n) { char junk; printf("Enter name: "); scanf("%s %c%c %s", n->first, &n->middle, &junk, n->last); 6/30/2019 ECE Application Programming: Lecture 32

6 Single SINew functions
void printStudent(SINew *s) { printName(&s->sname); printf("ID #%.8u\n", s->ID); printf("GPA: %.2lf\n", s->GPA); } 6/30/2019 ECE Application Programming: Lecture 32

7 Single SINew functions (cont.)
void readStudent(SINew *s) { readName(&s->sname); printf("Enter ID #: "); scanf("%u", &s->ID); printf("Enter GPA: "); scanf("%lf", &s->GPA); } 6/30/2019 ECE Application Programming: Lecture 32

8 ECE Application Programming: Lecture 32
printList() void printList(SINew list[], int n) { int i; // Loop index for (i = 0; i < n; i++) { printStudent(&list[i]); printf("\n"); } } 6/30/2019 ECE Application Programming: Lecture 32

9 ECE Application Programming: Lecture 32
findByLName() int findByLName(SINew list[], int n, char lname[]) { int i; // Loop index // Search for student with matching name // in list for (i = 0; i < n; i++) { if (strcmp(lname, list[i].sname.last) == 0) return i; } // If end of loop reached, student wasn’t // found return -1; 6/30/2019 ECE Application Programming: Lecture 32

10 ECE Application Programming: Lecture 32
findByID() int findByID(SINew list[], int n, unsigned int sID) { int i; // Loop index // Search for student with // matching ID in list for (i = 0; i < n; i++) { if (sID == list[i].ID) return i; } // If end of loop reached, student wasn’t // found return -1; 6/30/2019 ECE Application Programming: Lecture 32

11 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 6/30/2019 ECE Application Programming: Lecture 32

12 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 6/30/2019 ECE Application Programming: Lecture 32

13 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" 6/30/2019 ECE Application Programming: Lecture 32

14 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 6/30/2019 ECE Application Programming: Lecture 32

15 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 } 6/30/2019 ECE Application Programming: Lecture 32

16 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 6/30/2019 ECE Application Programming: Lecture 32

17 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 6/30/2019 ECE Application Programming: Lecture 32

18 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); 6/30/2019 ECE Application Programming: Lecture 32

19 ECE Application Programming: Lecture 32
Next time Character and line I/O Reminders: remaining key dates 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 6/30/2019 ECE Application Programming: Lecture 32


Download ppt "EECE.2160 ECE Application Programming"

Similar presentations


Ads by Google