EECE.2160 ECE Application Programming

Slides:



Advertisements
Similar presentations
BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
Advertisements

Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
FILES Files types and operations. Files Files are used to store data Data can be Text (ASCII only: 0  127) Binary (Full range: 0  256) Each file resides.
CSCI 171 Presentation 12 Files. Working with files File Streams – sequence of data that is connected with a specific file –Text Stream – Made up of lines.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
1 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
ECE 103 Engineering Programming Chapter 44 File I/O Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
CS 261 – Recitation 7 Spring 2015 Oregon State University School of Electrical Engineering and Computer Science.
Files A collection of related data treated as a unit. Two types Text
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
File Input/Output. File information Name z:\Visual Studio 2005\Projects\fileio\fileio\myinput.txt \\file-svr-1\class\ECE-160\pviall\myoutput.txt Read/Write.
ECE Application Programming
EECE.2160 ECE Application Programming
ECE Application Programming
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
ECE Application Programming
EECE.2160 ECE Application Programming
File Access (7.5) CSE 2031 Fall July 2018.
File I/O.
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
CS111 Computer Programming
CSE1320 Files in C Dr. Sajib Datta
Programming in C Input / Output.
Lecture 15 Files.
Manipulating File IO in Visual C++
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Input/Output and the Operating Systems
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
File Input and Output.
File Handling.
EECE.2160 ECE Application Programming
Files.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
ETE 132 Lecture 8 By Munirul Haque.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
CSc 352 File I/O Saumya Debray Dept. of Computer Science
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
ICS103: Programming in C 6: Pointers and Modular Programming
EECE.2160 ECE Application Programming
Professor Jodi Neely-Ritz University of Florida
Files Chapter 8.
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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