Presentation is loading. Please wait.

Presentation is loading. Please wait.

Module 12 Input and Output

Similar presentations


Presentation on theme: "Module 12 Input and Output"— Presentation transcript:

1 Module 12 Input and Output

2 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 @Copyright UMBC Training Centers 2012

3 @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 @Copyright UMBC Training Centers 2012

4 @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] @Copyright UMBC Training Centers 2012

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

6 @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 @Copyright UMBC Training Centers 2012

7 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 @Copyright UMBC Training Centers 2012

8 @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 @Copyright UMBC Training Centers 2012

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

10 @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 @Copyright UMBC Training Centers 2012

11 @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 @Copyright UMBC Training Centers 2012

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

13 @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 @Copyright UMBC Training Centers 2012

14 @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 @Copyright UMBC Training Centers 2012

15 @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 @Copyright UMBC Training Centers 2012

16 @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* @Copyright UMBC Training Centers 2012

17 @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 @Copyright UMBC Training Centers 2012

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

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

20 @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 @Copyright UMBC Training Centers 2012

21 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 @Copyright UMBC Training Centers 2012

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

23 @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 @Copyright UMBC Training Centers 2012

24 @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 @Copyright UMBC Training Centers 2012

25 @Copyright UMBC Training Centers 2012
Exercises Ex2 Ex4 @Copyright UMBC Training Centers 2012

26 @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 @Copyright UMBC Training Centers 2012

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

28 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 @Copyright UMBC Training Centers 2012

29 @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 @Copyright UMBC Training Centers 2012

30 @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 Lupoli Not Legit Lupoli 23 Lupoli Notice the error message that appears @Copyright UMBC Training Centers 2012

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

32 @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 & @Copyright UMBC Training Centers 2012

33 @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. @Copyright UMBC Training Centers 2012

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

35 @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) @Copyright UMBC Training Centers 2012

36 @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. @Copyright UMBC Training Centers 2012


Download ppt "Module 12 Input and Output"

Similar presentations


Ads by Google