1 / 27

C++ I /O

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.

deliz
Télécharger la présentation

C++ I /O

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ I /O INPUT AND OUTPUT STREAMS

  2. 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.

  3. 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>

  4. 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.

  5. 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.

  6. C++ Stream Classes • An I/O library provides two templates one for 8 bit character and another for wide character(16 bits).

  7. Template classes of C++ I/O

  8. C++ Streams

  9. Formatted I/O flag list

  10. 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.

  11. 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.

  12. 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.

  13. 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.

  14. 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.

  15. 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.

  16. 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.

  17. 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.

  18. 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.

  19. Examples char c; cin.get( c); // reads and assigns to c if(c==‘\n’) cout<<“ Stop reading”;

  20. example char c; c= cin.get() ; //reads and returns if(c==‘\n’) cout<<“ Stop reading”;

  21. 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.

  22. 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.

  23. 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.

  24. 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.

  25. Formatted Console I/O operations • C++ supports number of methods to format the output . • iosclass functions and flags. • manipulators. • User defined output functions.

  26. 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.

  27. manipulators • setw() equivalent to width(). • setprecision() ------- precision(). • setfill() ------- fill(). • setiosflags() ------- setf(). • resetiosflags() ----- unsetf().

More Related