290 likes | 522 Views
C++ I /O. INPUT AND OUTPUT STREAMS. INTRODUCTION. C++ supports two IO systems , one from C and another from C++(objects related). C and C++ I/O differs only in console I/O and disk I/O. That is reading and writing functions. MORE!. C provides rich, flexible and powerful I/O systems.
E N D
C++ I /O INPUT AND OUTPUT STREAMS
INTRODUCTION • C++ supports two IO systems , one from C and another from C++(objects related). • C and C++ I/O differs only in console I/O and disk I/O. • That is reading and writing functions.
MORE!.. • C provides rich, flexible and powerful I/O systems. • C++ provides its own I/O for manipulate user defined objects. • C++ uses C I/O for compatibility aspects. • C++ provides two I/O library, <iostream.h> and new one <iostream>
MORE!.. • The <iostream> is improved version of old library. • Major difference is new library has many new functions and data types. • Second Old library uses Global namespace and new one uses std namespace.
Stream ?..... • C and C++ I/O operates through Streams. • A stream is a logical device that either produces or consumes information. • A stream is linked to physical devices by I/O systems. • All streams are behave same even though actual physical device is connected different.
C++ Stream Classes • An I/O library provides two templates one for 8 bit character and another for wide character(16 bits).
More!.. • ios::skipws -> • When the skipws flag is set, leading white-space characters (spaces, tabs, andnewlines) are discarded when performing input on a stream. • When skipws is cleared, white-space characters are not discarded.
When the left flag is set, output is left justified. • When right is set, output is right justified. • By default, numeric values are output in decimal. • Setting the oct flag causes output to be displayed in octal. • Setting the hex flag causes output to be displayed in hexadecimal. • To return output to decimal, set the dec flag.
Setting showbase causes the base of numeric values to be shown. • For example, if the conversion base is hexadecimal, the value 1F will be displayed as 0x1F. • By default, when scientific notation is displayed, the e is in lowercase. • Also, when a hexadecimal value is displayed, the x is in lowercase. When uppercase is set, these characters are displayed in uppercase.
Setting showpos causes a leading plus sign to be displayed before positive values. • Setting showpointcauses a decimal point and trailing zeros to be displayed for all floating-point output—whether needed or not. • By setting the scientific flag, floating-point numeric values are displayed using scientific notation. • When fixed is set, floating-point values are displayed using normal notation.
When unitbuf is set, the buffer is flushed after each insertion operation. • When boolalphais set, Booleans can be input or output using the keywords true and false. • Since it is common to refer to the oct, dec, and hex fields, they can be collectively referred to as basefield. Similarly, the left, right, and internal fields can be referred to as adjustfield. • Finally, the scientific and fixed fields can be referenced as floatfield.
Unformatted I/O operations • Input and Output operations are achieved by using cin, coutand overloaded operators >> ,<<. • >> is overloaded in istream class. • << is overloaded in ostream class. • General form : cin>> var1 >> var2>>……..>>varn; where var1,var2,…. are variables declared already.
This statement stops the execution and look for data from the keyboard. • Input will be , • data1 data2 data3 ….. datan • Input data are separated by space and should match the type of the variable. • The new line(\n) , tab(\t) and white spaces are skipped while reading data. • Operator >> reads character by character and assigns to indicated location.
The reading for a variable will be terminated at the encounter of a white space or a character does not match the destination type. • Then the excluded data will remains in the input stream.
put() and get() functions • get() is a member function of istream class. • put is a member function of ostream class. • Two versions of get() method is available: • get(char *) and get (void). • get(char *) -> reads single character from the keyboard and assigns to an argument. • get(void) -> reads single character and returns it to the left side variable.
Examples char c; cin.get( c); // reads and assigns to c if(c==‘\n’) cout<<“ Stop reading”;
example char c; c= cin.get() ; //reads and returns if(c==‘\n’) cout<<“ Stop reading”;
put() function • put() function is used to display single character on the screen. • syntax : • cout.put(‘x’); // displays x on the screen. • cout.put(c);//displays value of variable c. • cout.put(68);// dispalys D on the screen.
getline() and write() • It is possible to read a line of text form the keyboard. • getline() function reads the line of text and reading ends when it reaches new line character. • Syntax : • cin.getline(line ,size); • where ‘line’ is array of character and sise is maximum number of characters.
More!... • The reading is terminated either by reaching new line or reaching size-1 , whichever is first. • The new line character is read and nor saved instead of that null character is added at the end. • The spaces are also be counted as characters.
write() • The write() method displays entire line on the screen. • The write methods reads the character by character untill it reaches the new line. • Syntax : • cout.write( line ,size); • Where line is character array and size is number of character to print.
Formatted Console I/O operations • C++ supports number of methods to format the output . • iosclass functions and flags. • manipulators. • User defined output functions.
ios -format functions • width -> To specify required field size to display an output value. • precision-> number of digits to be displayed after the decimal point. • fill() ->to specify the character to fill unused portion of a field. • setf() -> To set the required flag values. • unsetf() ->To clear the flag specified.
manipulators • setw() equivalent to width(). • setprecision() ------- precision(). • setfill() ------- fill(). • setiosflags() ------- setf(). • resetiosflags() ----- unsetf().