C++ io manipulation

2,368 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,368
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
85
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

C++ io manipulation

  1. 1. Lect 27 P. 1<br />Winter Quarter<br />C++ I/O Manipulation<br />
  2. 2. Lect 27 P. 2<br />Note: There is no “.h” on standard header files.<br /> Be careful about “using namespace std”<br />iostream -- contains basic information required for all stream I/O operations<br />iomanip -- contains information useful for performing formatted I/O with parameterized stream manipulators<br />fstream -- contains information for performing file I/O operations<br />strstream -- contains information for performing in-memory I/O operations (i.e., into or from strings in memory)<br />Winter Quarter<br />Stream I/O Library Header Files<br />
  3. 3. Lect 27 P. 3<br />ios is the base class.<br />istream and ostream inherit from ios<br />ifstream inherits from istream (and ios)<br />ofstream inherits from ostream (and ios)<br />iostream inherits from istream and ostream (& ios)<br />fstream inherits from ifstream, iostream, and ofstream<br />Winter Quarter<br />Classes for Stream I/O in C++<br />
  4. 4. Lect 27 P. 4<br />C++ provides various stream manipulators that perform formatting tasks.<br />Stream manipulators are defined in <iomanip><br />These manipulators provide capabilities for<br />setting field widths,<br />setting precision,<br />setting and unsetting format flags,<br />flushing streams,<br />inserting a "newline" and flushing output stream,<br />skipping whitespace in input stream<br />Winter Quarter<br />C++ Stream I/O -- Stream Manipulators<br />
  5. 5. Lect 27 P. 5<br />setprecision ( )<br />Select output precision, i.e., number of significant digits to be printed.<br />Example:<br /> cout << setprecision (2) ; // two significant digits<br />setw ( ) <br />Specify the field width (Can be used on input or output, but only applies to next insertion or extraction).<br />Example:<br /> cout << setw (4) ; // field is four positions wide<br />Winter Quarter<br />C++ Stream I/O -- Stream Manipulators<br />
  6. 6. Lect 27 P. 6<br />Winter Quarter<br />C++ Stream I/O -- Stream Format States<br />Various ios format flags specify the kinds of formatting to be performed during stream I/O. <br />There are member functions (and/or stream manipulators) which control flag settings.<br />There are various flags for trailing zeros and decimal points, justification, number base, floating point representation, and so on.<br />
  7. 7. Lect 27 P. 7<br />Stream I/O Format State Flags<br />ios::showpoint when set, show trailing decimal<br /> point and zeros<br />ios::showpos when set, show the + sign before<br /> positive numbers<br />ios::basefield <br /> ios::dec use base ten<br /> ios::oct use base eight<br /> ios::hex use base sixteen<br />Winter Quarter<br />
  8. 8. Lect 27 P. 8<br />Winter Quarter<br />Stream I/O Format State Flags<br />ios::floatfield<br /> ios::fixed use fixed number of digits<br /> ios::scientific use "scientific" notation<br />ios::adjustfield<br /> ios::left use left justification<br /> ios::right use right justification<br /> ios::internal left justify the sign, but right<br /> justify the value <br /> <br />
  9. 9. Lect 27 P. 9<br />ios::eofbit set when eof encountered [ stream.eof() ]<br />ios::failbit set when format error occurred on the<br /> stream, but no characters were lost<br /> [ stream.fail() or simply ! stream ]<br />ios::badbit set when stream error occurs that<br /> results in a loss of data [ stream.bad() ]<br />ios::goodbit set when none of the bits eofbit,<br />failbit, or badbit are set [ stream.good() ]<br />Winter Quarter<br />Stream I/O Format State Flags<br />
  10. 10. Lect 27 P. 10<br />.setf ( )<br />Allows the setting of an I/O stream format flag.<br />Examples:<br /> // To show the + sign in front of positive numbers<br />cout.setf (ios::showpos) ; <br /> // To output the number in hexadecimal<br /> cout.setf (ios::hex, ios::basefield) ; <br />Winter Quarter<br />I/O Stream Member Functions<br />
  11. 11. Lect 27 P. 11<br />.precision ( ) ;<br />Select output precision, i.e., number of significant digits to be printed.<br />Example:<br /> cout.precision (2) ; // two significant digits<br />.width ( ) ;<br />Specify field width. (Can be used on input or output, but only applies to next insertion or extraction).<br />Example:<br /> cout.width (4) ; // field is four positions wide<br />Winter Quarter<br />I/O Stream Member Functions<br />
  12. 12. Lect 27 P. 12<br />.eof ( ) ;<br />Tests for end-of-file condition.<br />Example:<br /> cin.eof ( ) ; // true if eof encountered<br />.fail ( ) ;<br />Tests if a stream operation has failed.<br />Example:<br /> cin.fail ( ) ; // true if a format error occurred<br /> ! cin; // same as above; true if format error<br />Winter Quarter<br />I/O Stream Member Functions<br />
  13. 13. Lect 27 P. 13<br />.clear ( ) ;<br />Normally used to restore a stream's state to "good" so that I/O may proceed or resume on that stream. <br />Example:<br /> cin.clear ( ) ; // allow I/O to resume on a "bad" <br /> // stream, in this case "cin", <br /> // on which error had previously<br /> // occurred<br />Winter Quarter<br />I/O Stream Member Functions<br />
  14. 14. Lect 27 P. 14<br />#include <iostream> // No “.h” (standard header)<br />#include <iomanip> // No “.h” (standard header)<br />using namespace std; // To avoid “std::”<br />int main ( )<br />{<br />int a, b, c = 8, d = 4 ;<br />float k ;<br />char name[30] ;<br />cout<< "Enter your name" <<endl;<br />  cin.getline (name, 30);<br />cout<< "Enter two integers and a float " <<endl;<br /> cin>> a >> b >> k ;<br />Winter Quarter<br />Using Manipulators & Member Functions<br />
  15. 15. Lect 27 P. 15<br />// Now, let's output the values that were read in<br />cout<< "nThank you, " << name << ", you entered"<br /><<endl<< a << ", " << b << ", and " ;<br />cout.width (4) ;<br />cout.precision (2) ;<br />cout<< k <<endl;<br />// Control the field and precision another way<br />cout<<"nThank you, " << name << ", you entered"<br /><<endl<< a << ", " << b << ", and " <<setw(c)<br /><<setprecision(d)<< k <<endl; <br />}<br />Winter Quarter<br />Using Manipulators & Member Functions<br />
  16. 16. Lect 27 P. 16<br />Winter Quarter<br />Example Program Output<br />Enter your name <br />R. J. Freuler <br />Enter two integers and a float <br />12 24 67.85 <br />Thank you, R. J. Freuler, you entered <br />12, 24, and 68 <br />Thank you, R. J. Freuler, you entered <br />12, 24, and 67.85<br />
  17. 17. Lect 27 P. 17<br />.get ( ) ;<br />Example:<br /> char ch ;<br />ch= cin.get ( ) ; // gets one character from keyboard<br /> // & assigns it to the variable "ch"<br />.get (character) ;<br />Example:<br /> char ch ;<br /> cin.get (ch) ; // gets one character from<br /> // keyboard & assigns to "ch"<br />Winter Quarter<br />More Input Stream Member Functions<br />
  18. 18. Lect 27 P. 18<br />Winter Quarter<br />More Input Stream Member Functions<br />.get (array_name, max_size) ;<br />Example:<br />char name[40] ;<br />cin.get(name, 40) ; // Gets up to 39 characters<br /> // and inserts a null at the end of the<br /> // string "name". If a delimiter is <br /> // found, the read terminates. The<br /> // delimiter is not stored in the array, // but it is left in the stream. <br />
  19. 19. Lect 27 P. 19<br />.getline (array_name, max_size) ;<br />Example:<br />char name[40] ;<br />cin.getline(name, 40) ; // Gets up to 39 characters<br /> // and assigns the string to "name". A<br /> // null is inserted at the end of the string.<br /> // Note that if a delimiter is found,<br /> // it is removed from the stream, but it is<br /> // not stored in the character array. <br />Winter Quarter<br />More Input Stream Member Functions<br />
  20. 20. Lect 27 P. 20<br />More Input Stream Member Functions<br />.ignore ( ) ;<br />Ex:<br />cin.ignore ( ) ; // gets and discards 1 character<br />cin.ignore(2) ; // gets and discards 2 characters<br />cin.ignore (80, 'n'); // gets and discards up to 80<br /> // characters or until "newline"<br /> // character, whichever comes<br /> // first<br />Winter Quarter<br />
  21. 21. Lect 27 P. 21<br />More Input Stream Member Functions<br />.peek( ) ;<br />Ex:<br />char ch ;<br /> ch = cin.peek ( ) ; // peek at (don't take) character<br />.putback( ) ;<br />Ex:<br />char ch;<br />cin.putback(ch); // put character back in stream<br />Winter Quarter<br />
  22. 22. Lect 27 P. 22<br />More I/O Stream Member Functions<br />Winter Quarter<br />.read( ) ; .write( ) ;<br />Ex:<br />char gross[144] ;<br />cin.read(gross,144); // reads 144 characters from<br /> // input stream. Does NOT<br /> // append '0'<br />
  23. 23. Lect 27 P. 23<br />File I/O with C++<br />#include <fstream><br />using namespace std;<br />int main ( ) {<br />int a, b, c ;<br />ifstream fin ; //Create file input stream object<br /> fin.open( "my_input.dat"); //Open input file<br /> fin >> a >> b ; //Read two values from input file<br /> c = a + b ;<br />ofstream fout ; //Create file output stream object<br /> fout.open( "my_output.dat"); //Open output file<br /> fout << c <<endl; //Write result to output file<br /> fin.close ( ) ; //Close input file<br /> fout.close( ) ; //Close output file<br />}<br />Winter Quarter<br />

×