EECE.2160 ECE Application Programming

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 ICS103 Programming in C Lecture 8: Data Files. 2 Outline Why data files? Declaring FILE pointer variables Opening data files for input/output Scanning.
1 CSE1301 Computer Programming: Lecture 9 Input/Output.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Lecture 11: Files & Arrays B Burlingame 18 November 2015.
Using Files Declare a variable, called file pointer, of type FILE * Use function fopen to open a named file and associate this file with the file pointer.
CSE1301 Computer Programming: Lecture 6 Input/Output.
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.
1 Computer Programming Lecture 15 Text File I/O Assist. Prof Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
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
ECE Application Programming
ECE Application Programming
ECE Application Programming
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
ECE Application Programming
Structured Programming II
ECE Application Programming
ECE Application Programming
Computer Programming Lecture 15 Text File I/O
Programming in C Input / Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
CSI 121 Structured Programming Language Lecture 7: Input/Output
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
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
FILE handeling.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
ETE 132 Lecture 8 By Munirul Haque.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
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
EECE.2160 ECE Application Programming
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
Presentation transcript:

EECE.2160 ECE Application Programming Instructor: Dr. Michael Geiger Spring 2018 Lecture 29: File I/O (continued)

ECE Application Programming: Lecture 29 Lecture outline Announcements/reminders Program 6 graded; regrades due Wed 4/18 Program 7 due today Program 8 to be posted; due 4/23 No lecture Monday 4/16 (Patriots Day) Today’s class Continue with file I/O Character & line I/O 5/6/2019 ECE Application Programming: Lecture 29

ECE Application Programming: Lecture 29 Review: File I/O Open file: FILE *fopen(filename, file_access) Returns NULL if file can’t be opened Close file: fclose(file_handle) 5/6/2019 ECE Application Programming: Lecture 29

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

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); 5/6/2019 ECE Application Programming: Lecture 29

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

ECE Application Programming: Lecture 29 The program (part 1) #include <stdio.h> void main() { FILE *infile; // Input file pointer FILE *outfile; // Output file pointer int x, y, z, sum; // Input values and sum double avg; // Average of x, y, and z // Open input file, exit if error infile=fopen("myinput.txt","r"); if (infile==NULL) printf("Error opening myinput.txt\n"); return; } // Can actually open file as part of conditional statement if ((outfile=fopen("myoutput.txt","w"))==NULL) printf("Error opening myoutput.txt\n"); 5/6/2019 ECE Application Programming: Lecture 29

ECE Application Programming: Lecture 29 The program (part 2) // Read the three values fscanf(infile, "%d %d %d", &x, &y, &z); // Compute sum and average sum = x + y + z; avg = sum / 3.0; // print out values fprintf(outfile, "Values: %d, %d, %d\n", x, y, z); fprintf(outfile, "Sum: %d\n",sum); fprintf(outfile, "Avg: %lf\n",avg); // close the files fclose(infile); fclose(outfile); } 5/6/2019 ECE Application Programming: Lecture 29

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

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

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

ECE Application Programming: Lecture 29 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); 5/6/2019 ECE Application Programming: Lecture 29

ECE Application Programming: Lecture 29 Next time Character and line I/O Reminders: Program 6 graded; regrades due Wed 4/18 Program 7 due today Program 8 to be posted; due 4/23 No lecture Monday 4/16 (Patriots Day) 5/6/2019 ECE Application Programming: Lecture 29