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