EECE.2160 ECE Application Programming

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
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.
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.
Files Programs and data are stored on disk in structures called files Examples Turbo C++ - binary file Word binary file lab1.c - text file lab1.data.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
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.
File IO and command line input CSE 2451 Rong Shi.
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:
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CS 261 – Recitation 7 Spring 2015 Oregon State University School of Electrical Engineering and Computer Science.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
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.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
CS 261 – Recitation 7 Fall 2013 Oregon State University
Plan for the Day: I/O (beyond scanf and printf)
CSE1320 Files in C Dr. Sajib Datta
CSE1320 Files in C Dr. Sajib Datta
Files I/O, Streams, I/O Redirection, Reading with fscanf
File Input and Output.
File Handling.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Line at a time I/O with fgets() and fputs()
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.
Module 12 Input and Output
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
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
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
Professor Jodi Neely-Ritz University of Florida
EECE.2160 ECE Application Programming
Presentation transcript:

EECE.2160 ECE Application Programming Instructor: Dr. Michael Geiger Fall 2018 Lecture 34: Character & line I/O

ECE Application Programming: Lecture 34 Lecture outline Announcements/reminders No Thursday office hours this week Today: 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: binary file I/O Character & line I/O 6/11/2019 ECE Application Programming: Lecture 34

Review: binary file 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 6/11/2019 ECE Application Programming: Lecture 34

Review: binary file 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); 6/11/2019 ECE Application Programming: Lecture 34

Review: 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); 6/11/2019 ECE Application Programming: Lecture 34

ECE Application Programming: Lecture 34 Program 8 overview Program reads two binary input files Represent up to 20 resistors / voltage drops Should fill two more arrays Current through each resistor Power dissipated by each resistor Output printed to text file List of resistor/voltage pairs Table of min/max/avg values for voltage, current, & power 6/11/2019 ECE Application Programming: Lecture 34

ECE Application Programming: Lecture 34 Character I/O Output functions: send single character to output stream (fputc(), putchar()) Not significantly different than using printf()/fprintf() Input functions Read single character from input int fgetc(FILE *stream); int getchar(); Macro: #define getchar() fgetc(stdin) Return last character to input int ungetc(int c, FILE *stream); 6/11/2019 ECE Application Programming: Lecture 34

ECE Application Programming: Lecture 34 Common uses Read input character-by-character until EOF while ((ch = fgetc(fp)) != EOF) { … } Read character until it does not match format Example: read digits until first non-digit encountered while (isdigit(ch = fgetc(fp))) { … } ungetc(ch, fp); 6/11/2019 ECE Application Programming: Lecture 34

ECE Application Programming: Lecture 34 Line I/O Output functions (puts(), fputs()) Not significantly different than using printf()/fprintf() Input functions Could use scanf(“%[^\n]", str); Read line from stream: char *fgets(char *s, int n, FILE *stream); fgets() can limit # characters read Automatically null terminates, so it will read up to n-1 characters Will read newline 6/11/2019 ECE Application Programming: Lecture 34

ECE Application Programming: Lecture 34 Examples Show the output of the following short program Input: Test Input 1 23 4 5\n void main() { char c; char buffer[50]; int i; i = 0; while ((c = fgetc(stdin)) != '\n') { if (c != ' ') { buffer[i++] = c; } buffer[i] = '\0'; fputs(buffer, stdout); Output: TestInput12345 6/11/2019 ECE Application Programming: Lecture 34

ECE Application Programming: Lecture 34 Examples (cont.) Input: Test1 Test 2 abcdefghijklmnopqrstuvwxyz This is a test of the fgets() function void main() { char str[25]; int i; for (i = 0; i < 5; i++) { fgets(str, 24, stdin); strcat(str, "\n"); fputs(str, stdout); } Output: Test1 Test 2 abcdefghijklmnopqrstuvw xyz This is a test of the f 6/11/2019 ECE Application Programming: Lecture 34

ECE Application Programming: Lecture 34 Examples (cont.) Output: n = 1024, n * 2 = 2048 buffer = Some other stuff Input: 1024Some other stuff void main() { char c; char buffer[50]; int n = 0; // isdigit in <ctype.h> while (isdigit(c = getchar())) { n = n * 10 + (c - 48); // Hint: '0' = 48 } // (ASCII value) ungetc(c, stdin); fgets(buffer, 50, stdin); printf("n = %d, n * 2 = %d\n", n, n * 2); printf("buffer = %s\n", buffer); } 6/11/2019 ECE Application Programming: Lecture 34

ECE Application Programming: Lecture 34 Next time Character/line input examples Bitwise operators Reminders: No Thursday office hours this week Today: 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 6/11/2019 ECE Application Programming: Lecture 34