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

CS1061 C Programming Lecture 17: Steams and Character I/O A. O’Riordan, 2004.
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.
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.
Opening and Closing Files First define a file pointer, which can "pointing" to one of the opened file. #include... FILE *fp; Use fopen() to open a file,
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.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
File I/O, Project 1: List ADT Bryce Boe 2013/07/02 CS24, Summer 2013 C.
CSEB114: Principle of programming Chapter 11: Data Files & File Processing.
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.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
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.
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.
IO revisited CSE 2451 Rong Shi. stdio.h Functions – printf – scanf(normally stops at whitespace) – fgets – sscanf Standard streams – stdin(defaults to.
Advanced Programming in the UNIX Environment Hop Lee.
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
Lecture 11 File input/output
ECE Application Programming
Chapter 18 I/O in C.
CGS 3460, Lecture 41 Apr 21, 2006 Hen-I Yang
CS 261 – Recitation 7 Fall 2013 Oregon State University
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
CSE1320 Files in C Dr. Sajib Datta
Programming in C Input / Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
CSC215 Lecture Input and Output.
CSC215 Lecture Input and Output.
EECE.2160 ECE Application Programming
File Handling.
EECE.2160 ECE Application Programming
Line at a time I/O with fgets() and fputs()
Programming in C Input / 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
FILE handeling.
Module 12 Input and Output
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
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
I/O CS580U - Fall 2018.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

EECE.2160 ECE Application Programming Instructor: Dr. Michael Geiger Fall 2017 Lecture 30: Character/line I/O

ECE Application Programming: Lecture 30 Lecture outline Announcements/reminders Late/regrade submissions: e-mail Dr. Geiger + TA Zhendong Wang handles even-numbered programs, Lin Li handles odd-numbered programs Program 6 graded; regrades due 11/29 Program 7 late penalties increasing again as of today Program 8 due 12/4 Today’s class Review: file I/O basics, formatted & unformatted I/O Character and line I/O 2/16/2019 ECE Application Programming: Lecture 30

ECE Application Programming: Lecture 30 Review: File I/O Open file: FILE *fopen(filename, file_access) Returns NULL if file can’t be opened Close file: fclose(file_handle) Formatted I/O: fprintf(file_handle, format_specifier, 0+ variables) fscanf(file_handle, format_specifier, 0+ variables) 2/16/2019 ECE Application Programming: Lecture 30

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

Review: 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); 2/16/2019 ECE Application Programming: Lecture 30

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

ECE Application Programming: Lecture 30 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); 2/16/2019 ECE Application Programming: Lecture 30

ECE Application Programming: Lecture 30 Character I/O Output functions: send single character to output stream Not significantly different than using printf()/fprintf() int fputc(int c, FILE *stream); int putchar(int c); Macro: #define putchar(c) fputc((c), stdout) 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); 2/16/2019 ECE Application Programming: Lecture 30

ECE Application Programming: Lecture 30 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); 2/16/2019 ECE Application Programming: Lecture 30

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

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

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

ECE Application Programming: Lecture 30 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); } 2/16/2019 ECE Application Programming: Lecture 30

ECE Application Programming: Lecture 30 Next time Dynamic memory allocation Reminders: Late/regrade submissions: e-mail Dr. Geiger + TA Zhendong Wang handles even-numbered programs, Lin Li handles odd-numbered programs Program 6 graded; regrades due 11/29 Program 7 late penalties increasing again as of today Program 8 due 12/4 2/16/2019 ECE Application Programming: Lecture 30