Unformatted text preview:

I/O ManipulationStream I/O Library Header FilesClasses for Stream I/O in C++C++ Stream I/O -- Stream ManipulatorsSlide 5C++ Stream I/O -- Stream Format StatesStream I/O Format State FlagsSlide 8Slide 9I/O Stream Member FunctionsSlide 11Slide 12Slide 13Using Manipulators & Member FunctionsSlide 15Example Program OutputMore Input Stream Member FunctionsSlide 18Slide 19Slide 20Slide 21More I/O Stream Member FunctionsFile I/O with C++Lect 27 P. 1 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionI/O ManipulationLecture 27Lect 27 P. 2 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionStream I/O Library Header Files Note: There is no “.h” on standard header files.Be careful about “using namespace std”•iostream -- contains basic information required for all stream I/O operations•iomanip -- contains information useful for performing formatted I/O with parameterized stream manipulators•fstream -- contains information for performing file I/O operations•strstream -- contains information for performing in-memory I/O operations (i.e., into or from strings in memory)Lect 27 P. 3 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionClasses for Stream I/O in C++ ios is the base class.istream and ostream inherit from iosifstream inherits from istream (and ios)ofstream inherits from ostream (and ios)iostream inherits from istream and ostream (& ios)fstream inherits from ifstream, iostream, and ofstreamLect 27 P. 4 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionC++ Stream I/O -- Stream Manipulators•C++ provides various stream manipulators that perform formatting tasks.•Stream manipulators are defined in <iomanip>•These manipulators provide capabilities for–setting field widths,–setting precision,–setting and unsetting format flags,–flushing streams,–inserting a "newline" and flushing output stream,–skipping whitespace in input streamLect 27 P. 5 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionC++ Stream I/O -- Stream Manipulatorssetprecision ( )•Select output precision, i.e., number of significant digits to be printed.•Example:cout << setprecision (2) ; // two significant digitssetw ( ) •Specify the field width (Can be used on input or output, but only applies to next insertion or extraction).•Example:cout << setw (4) ; // field is four positions wideLect 27 P. 6 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionC++ Stream I/O -- Stream Format States•Various ios format flags specify the kinds of formatting to be performed during stream I/O. •There are member functions (and/or stream manipulators) which control flag settings.•There are various flags for trailing zeros and decimal points, justification, number base, floating point representation, and so on.Lect 27 P. 7 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionStream I/O Format State Flagsios::showpoint when set, show trailing decimal points and zerosios::showpos when set, show the + sign beforepositive numbersios::basefieldios::dec use base ten ios::oct use base eightios::hex use base sixteenLect 27 P. 8 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionStream I/O Format State Flagsios::floatfieldios::fixed use fixed number of digitsios::scientific use "scientific" notationios::adjustfieldios::left use left justificationios::right use right justificationios::internal left justify the sign, but rightjustify the valueLect 27 P. 9 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionStream I/O Format State Flagsios::eofbit set when eof encountered [ stream.eof() ]ios::failbit set when format error occurred on thestream, but no characters were lost[ stream.fail() or simply ! stream ]ios::badbit set when stream error occurs thatresults in a loss of data [ stream.bad() ]ios::goodbit set when none of the bits eofbit,failbit, or badbit are set [ stream.good() ]Lect 27 P. 10 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionI/O Stream Member Functions.setf ( )•Allows the setting of an I/O stream format flag.•Examples:// To show the + sign in front of positive numberscout.setf (ios::showpos) ; // To output the number in hexadecimalcout.setf (ios::hex, ios::basefield) ;Lect 27 P. 11 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionI/O Stream Member Functions.precision ( ) ;•Select output precision, i.e., number of significant digits to be printed.•Example:cout.precision (2) ; // two significant digits.width ( ) ;•Specify field width. (Can be used on input or output, but only applies to next insertion or extraction).•Example:cout.width (4) ; // field is four positions wideLect 27 P. 12 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionI/O Stream Member Functions.eof ( ) ;•Tests for end-of-file condition.•Example:cin.eof ( ) ; // true if eof encountered.fail ( ) ;•Tests if a stream operation has failed.•Example:cin.fail ( ) ; // true if a format error occurred! cin; // same as above; true if format errorLect 27 P. 13 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionI/O Stream Member Functions.clear ( ) ;•Normally used to restore a stream's state to "good" so that I/O may proceed or resume on that stream. •Example:cin.clear ( ) ; // allow I/O to resume on a "bad" // stream, in this case "cin", // on which error had previously // occurredLect 27 P. 14 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionUsing Manipulators & Member Functions#include <iostream> // No “.h” (standard header)#include <iomanip> // No “.h” (standard header)using namespace std; // To avoid “std::”int main ( ){ int a, b, c = 8, d = 4


View Full Document

OSU ENGR H192 - Lecture 27 - Manipulation

Documents in this Course
Strings

Strings

22 pages

Load more
Download Lecture 27 - Manipulation
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 27 - Manipulation and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 27 - Manipulation 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?