Console Io Operations


Published on

explains some of the character and string input output functions

1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Console Io Operations

  1. 1. CONSOLE I/O OPERATIONS Console I/O functions perform input from standard input devices and gives output to the standard output device of a system. Generally, keyboard is standard input device And monitor is standard output device
  2. 2. Header files and their purpose <ul><li>Certain functions in C++ are available in C++ header files. </li></ul><ul><li>Header file is included in the program using a preprocessor directive #include. </li></ul><ul><li>Preprocessor directive means it is an instruction for the compiler that it is to be carried out in the preprocessing stage. </li></ul><ul><li>Preprocessing stage means before the actual compilation of the program code. </li></ul>
  3. 3. #include directive <ul><li>Instructs the compiler to read and include the header file under the current file. </li></ul><ul><li>After including the specified file, the compiler compiles the total code ( i.e code of included file as well as current file ). </li></ul>
  4. 4. SINGLE CHARACTER FUNCTIONS <ul><li>getchar() and putchar() </li></ul><ul><li>Header file – stdio.h </li></ul><ul><li>Function is buffered i.e. Character typed by the user is not passed to the variable until the user hits the Enter key. </li></ul><ul><li>Since Enter key is itself a character and gets stored in the buffer. </li></ul><ul><li>To clear the input buffer fflush(stdin) function is to be used. </li></ul><ul><li>Putchar() is used to send single character to the output device. </li></ul><ul><li>It is also buffered , therefore to clean the output buffer function fflush(stdout) is used. </li></ul>
  5. 5. Example: Program using getchar() and putchar() function. Program to accept characters from the user till ‘n’ or ‘N’ is pressed. <ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>do </li></ul><ul><li>{ </li></ul><ul><li>char ch; </li></ul><ul><li>ch = getchar ( ); </li></ul><ul><li>fflush(stdin); </li></ul><ul><li>putchar(ch); </li></ul><ul><li>fflush(stdout); </li></ul><ul><li>} while((ch!=‘n’)&&(ch!=‘N’)); </li></ul><ul><li>} </li></ul>
  6. 6. getc() and putc( ) functions <ul><li>Header file: stdio.h </li></ul><ul><li>Mainly used with files </li></ul><ul><li>getc( stdin ) : Same as getchar( ) except that getchar( ) reads a character from the keyboard by default whereas stdin of function getc(stdin) make it read from a file represented by a standard input device. </li></ul><ul><li>putc(ch, stdout) : sends the character stored in ‘ch’ variable to standard output device. </li></ul>
  7. 7. getche( ) and putch( ) <ul><li>Header file: conio.h </li></ul><ul><li>This function does not wait fro the enter key to be pressed </li></ul><ul><li>Displays the input character ch </li></ul><ul><li>e stands for echo </li></ul><ul><li>Different from getch( ) function since getch() does not display the input character. </li></ul>
  8. 8. The classes istream (input stream ) and ostream (output stream) defined under iostream.h define some functions to handle character and string input/output operations. <ul><li>get( ) </li></ul><ul><li>When >> operator is used with cin, it cannot accept spaces and newline characters. </li></ul><ul><li>When cin.get(ch) is used, it reads spaces and newline character as well. </li></ul><ul><li>put( ) </li></ul><ul><li>cout.put(ch) </li></ul>
  9. 9. STRING BASED FUNCTIONS <ul><li>gets( ) and puts( ) </li></ul><ul><li>Header file – stdio.h </li></ul><ul><li>Reads a string from standard input device </li></ul><ul><li>Also buffered , therefore need to press enter key </li></ul><ul><li>Therefore, fflush needs to be used to clear IP buffer </li></ul><ul><li>Can accept spaces </li></ul><ul><li>Different from cin function because cin cannot accept spaces. </li></ul>
  10. 10. getline( ) function <ul><li>Reads a line of text </li></ul><ul><li>Syntax: cin.getline( var name, size) </li></ul><ul><li>Size is the total no. of characters including one null character : ‘’ </li></ul>