Module 12 Input and Output

Slides:



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

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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Programming in C #2 Input / Output.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
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 18 I/O in C.
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.
File I/O, Project 1: List ADT Bryce Boe 2013/07/02 CS24, Summer 2013 C.
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.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Basic I/O in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.
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.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
IO revisited CSE 2451 Rong Shi. stdio.h Functions – printf – scanf(normally stops at whitespace) – fgets – sscanf Standard streams – stdin(defaults to.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
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.
Chapter 7 Text Input/Output Objectives
TMF1414 Introduction to Programming
File Access (7.5) CSE 2031 Fall July 2018.
Chapter 18 I/O in C.
File I/O.
Introduction to C CSE 2031 Fall /3/ :33 AM.
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
C Programming:Part 3 Characters and Strings File Processing Exercise.
CSE1320 Files in C Dr. Sajib Datta
File Input/Output.
CSE1320 Files in C Dr. Sajib Datta
Programming in C Input / Output.
Programming in C Input / Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
CSE1320 Files in C Dr. Sajib Datta
File I/O We are used to reading from and writing to the terminal:
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
CSC215 Lecture Input and Output.
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Chapter 9 - Formatted Input/Output
Chapter 18 I/O in C.
File Input and Output.
File Handling.
Chapter 18 I/O in C.
Programming in C Input / Output.
Weeks 9-10 IO System Calls Standard IO (stdin, stdout) and Pipes
Introduction to C EECS May 2019.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Professor Jodi Neely-Ritz University of Florida
Professor Jodi Neely-Ritz University of Florida
File I/O We are used to reading from and writing to the terminal:
Chapter 18 I/O in C.
Presentation transcript:

Module 12 Input and Output

Communications Channels stdin – standard input device (keyboard) stdout – standard output device (monitor) stderr – standard error device (monitor) Defined in stdio.h Also includes prototypes for input/output functions www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Character I/O getchar( ) to read a single character from stdin putchar( char ) to output a single char to stdout www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Formatted output printf( “format string”, var1, var2, … ); Outputs to stdout “format string” consists of Literal text to be output Format specifiers for expressions General form of format specifiers %[flags][width][.precision][hlL][type] www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Common Flags Flag Meaning - Left justify + Output values with +/- Leading 0s for values www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Width and Precision Width number = minimum number of characters to output Precision .number = min number of digits for integer number of decimal places for float/double max characters for string www.umbctraining.com @Copyright UMBC Training Centers 2012

Types Common Type Specifiers Specifier To display %d, %i integer %ld long integer %hd short integer %u unsigned integer %f float/double %c A single character %s A NUL terminated string %p pointer %x, %X Hexadecimal integer %o Octal integer www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Let’s Take a Look C::B printDemo Go over basic printf output format strings – see comments in code Start with default output and uncomment each section as you go Make a copy or be sure to put comments back for next time www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Unformatted Output puts(char *string); Outputs string to stdout Appends a newline character to output www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Formatted Input scanf( “format string”, var1, var2, … ); Inputs from stdin “format string” consists of Literal text to be input Format specifiers for interpreting input Type specifiers same as printf Use %lf to input double www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Scanning input scanf skips leading whitespace Except for %c or bracketed chars Reads characters until max characters specified by field width are read An invalid character for the specified type is encountered Whitespace terminates a string (%s) Next scanf starts where previous scanf stopped Lots of scanf example in C::B www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Let’s Take a Look C::B Scanf1 Nothing new Scanf2 Nothing new, multiple scans in ONE line Scanf3 www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 I/O Redirection Output to stdout written to a file myProgram > outputFile Input from stdin read from a file myProgram < inputFile Redirect both input and output myProgram < inputFile > outputFile www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Streams In C, all I/O is performed using streams A sequence of bytes that “flows” between your program and an I/O device A high level abstraction representing a channel to a file or I/O device Some I/O functions such as printf and scanf use the standard streams (stdout/stdin) by default opened when your program begins execution www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Lots of Streams C Application Keyboard data.txt Monitor output.txt stdin FILE* stdout I/O Device I/O Device stderr www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 File I/O Files must be opened to create a stream before I/O may be performed Functions that perform I/O with files are similar to those for stdout/stdin, but must specify the stream to which the I/O is performed Streams are designated as type FILE* www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Opening a Stream FILE* fopen(char fileName[], char mode[]); fileName is the path to the file Windows: C:\folder\filename Unix: ../directory/filename mode is a 1- or 2-character string Return value FILE* is the stream used as the argument to other functions Returns NULL if an error occurs www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Closing a Stream fclose(FILE* stream); stream is FILE* returned from fopen( ) www.umbctraining.com @Copyright UMBC Training Centers 2012

Formatted Stream Output fprintf(FILE* stream, “formatString”, var1, var2,…); stream is FILE* returned from fopen( ) “formatString” is the same as in printf() www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Let’s Take a Look C::B fprintf could get filename from user ERROR MSGS – user fprintf(stderr,….) rather than printf —line 24 www.umbctraining.com @Copyright UMBC Training Centers 2012

Formatted Stream Input fscanf(FILE* stream, “formatString”, &var1, &var2,…); stream is FILE* returned from fopen( ) “formatString” is the same as in scanf() Returns EOF if error or end-of-file www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Let’s take a look C::B – fscanf Nothing new Checks for EOF www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Unformatted Input int getc(FILE* stream) int fgetc(FILE* stream) Return a character from stream as int Return EOF if error or end-of-file fgets(char line[], int maxChars, FILE* stream); inputs a line from a stream Reads up to maxChars - 1 characters fgets( )with STDIN as the stream is a good technique Returns NULL if error or end-of-file www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Let’s take a look C::B fgets Change LINE_SIZE to <= 17 and run again Also shows puts( ) Note double spacing – one \n from file, one from puts Change while-loop to use commented code (fgetc), then to getc www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Exercises Ex2 Ex4 www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Unformatted Output putc(int c, FILE* stream) fputc(int c, FILE* stream) Output an unformatted character to stream fputs(char string[], FILE* stream); Output an unformatted string to stream www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Let’s take a look C::B fputs Fputs (outputs a String) Fputc (outputs a Character) www.umbctraining.com @Copyright UMBC Training Centers 2012

Other stream Functions int ungetc( int c, FILE* stream) Pushes c back onto the stream Cannot push EOF Returns char pushed or EOF on error int fflush( FILE *stream ) For output stream, causes buffered output to be written For input stream, undefined www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 sscanf( ) sscanf(char string[], “formatString”, &var1, &var2, …); “reads” input from string Returns the number of items successfully read Combine with fgets for input error checking www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Let’s take a look C::B sscanf Notice that input MUST be all in ONE line with spaces Legit 23 156 Lupoli Not Legit Lupoli 23 Lupoli Notice the error message that appears www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 sprintf() sprintf(char string[], “formatString”, var1, var2, …); “writes” to string Much like strcat Otherwise just like printf www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Let’s take a look C::B sprintf Notice that “line will have two \n s!!! Also notice that ADDING “age” requires & www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 I/O Function Summary Stream Input Stream Output stdin stdout char getc, fgetc fscanf with %c putc, fputc fprintf with %c getchar scanf with %c putchar printf with %c string fscanf with %s fputs fprintf with %s scanf with %s puts printf with %s line fgets Same as string Stream functions may also be used with stdin, stdout and stderr. www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 DOs and DON’Ts DON’T use gets( ) DON’T use scanf, fscanf DO use fgets with sscanf www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Exercises Ex1-NameAgeLetter – input int, char, string Ex3-MileConversion- formatted table output Textbook – pg 371 #6 – display file 20 lines at a time Ex5-FileIO – reading and comparing strings (from PE5-2) www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 If you have any comments about this video, please use the contact information in your registration materials to let us know. www.umbctraining.com @Copyright UMBC Training Centers 2012