Engineering H192 - Computer Programming I/O Manipulation Lecture 27 Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 27 P.

Download Report

Transcript Engineering H192 - Computer Programming I/O Manipulation Lecture 27 Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 27 P.

Engineering H192 - Computer Programming
I/O Manipulation
Lecture 27
Winter Quarter
The Ohio State University
Gateway Engineering Education Coalition
Lect 27
P. 1
Engineering H192 - Computer Programming
Stream 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 inmemory I/O operations (i.e., into or from strings in
memory)
Winter Quarter
The Ohio State University
Gateway Engineering Education Coalition
Lect 27
P. 2
Engineering H192 - Computer Programming
Classes for Stream I/O in C++
ios is the base class.
istream and ostream inherit from ios
ifstream inherits from istream (and ios)
ofstream inherits from ostream (and ios)
iostream inherits from istream and ostream (& ios)
fstream inherits from ifstream, iostream, and ofstream
Winter Quarter
The Ohio State University
Gateway Engineering Education Coalition
Lect 27
P. 3
Engineering H192 - Computer Programming
C++ 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 stream
Winter Quarter
The Ohio State University
Gateway Engineering Education Coalition
Lect 27
P. 4
Engineering H192 - Computer Programming
C++ Stream I/O -- Stream Manipulators
setprecision ( )
• Select output precision, i.e., number of significant
digits to be printed.
• Example:
cout << setprecision (2) ; // two significant digits
setw ( )
• 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 wide
Winter Quarter
The Ohio State University
Gateway Engineering Education Coalition
Lect 27
P. 5
Engineering H192 - Computer Programming
C++ 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.
Winter Quarter
The Ohio State University
Gateway Engineering Education Coalition
Lect 27
P. 6
Engineering H192 - Computer Programming
Stream I/O Format State Flags
ios::showpoint
when set, show trailing decimal
point and zeros
ios::showpos
when set, show the + sign before
positive numbers
ios::basefield
ios::dec
ios::oct
ios::hex
Winter Quarter
use base ten
use base eight
use base sixteen
The Ohio State University
Gateway Engineering Education Coalition
Lect 27
P. 7
Engineering H192 - Computer Programming
Stream I/O Format State Flags
ios::floatfield
ios::fixed
ios::scientific
ios::adjustfield
ios::left
ios::right
ios::internal
Winter Quarter
use fixed number of digits
use "scientific" notation
use left justification
use right justification
left justify the sign, but right
justify the value
The Ohio State University
Gateway Engineering Education Coalition
Lect 27
P. 8
Engineering H192 - Computer Programming
Stream I/O Format State Flags
ios::eofbit
set when eof encountered [ stream.eof() ]
ios::failbit
set when format error occurred on the
stream, but no characters were lost
[ stream.fail() or simply ! stream ]
ios::badbit set when stream error occurs that
results in a loss of data [ stream.bad() ]
ios::goodbit set when none of the bits eofbit,
failbit, or badbit are set [ stream.good() ]
Winter Quarter
The Ohio State University
Gateway Engineering Education Coalition
Lect 27
P. 9
Engineering H192 - Computer Programming
I/O Stream Member Functions
.setf ( )
• Allows the setting of an I/O stream format flag.
• Examples:
// To show the + sign in front of positive numbers
cout.setf (ios::showpos) ;
// To output the number in hexadecimal
cout.setf (ios::hex, ios::basefield) ;
Winter Quarter
The Ohio State University
Gateway Engineering Education Coalition
Lect 27
P. 10
Engineering H192 - Computer Programming
I/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 wide
Winter Quarter
The Ohio State University
Gateway Engineering Education Coalition
Lect 27
P. 11
Engineering H192 - Computer Programming
I/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 error
Winter Quarter
The Ohio State University
Gateway Engineering Education Coalition
Lect 27
P. 12
Engineering H192 - Computer Programming
I/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 ( ) ;
Winter Quarter
// allow I/O to resume on a "bad"
// stream, in this case "cin",
// on which error had previously
// occurred
The Ohio State University
Gateway Engineering Education Coalition
Lect 27
P. 13
Engineering H192 - Computer Programming
Using 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 ;
float k ;
char name[30] ;
cout << "Enter your name" << endl ;
cin.getline (name, 30) ;
cout << "Enter two integers and a float " << endl ;
cin >> a >> b >> k ;
Winter Quarter
The Ohio State University
Gateway Engineering Education Coalition
Lect 27
P. 14
Engineering H192 - Computer Programming
Using Manipulators & Member Functions
// Now, let's output the values that were read in
cout << "\nThank you, " << name << ", you entered"
<< endl << a << ", " << b << ", and " ;
cout.width (4) ;
cout.precision (2) ;
cout << k << endl ;
// Control the field and precision another way
cout <<"\nThank you, " << name << ", you entered"
<< endl << a << ", " << b << ", and " << setw (c)
<< setprecision (d) << k << endl ;
}
Winter Quarter
The Ohio State University
Gateway Engineering Education Coalition
Lect 27
P. 15
Engineering H192 - Computer Programming
Example Program Output
Enter your name
R. J. Freuler
Enter two integers and a float
12 24 67.85
Thank you, R. J. Freuler, you entered
12, 24, and •
68
Thank you, R. J. Freuler, you entered
12, 24, and •
67.85
Winter Quarter
The Ohio State University
Gateway Engineering Education Coalition
Lect 27
P. 16
Engineering H192 - Computer Programming
More Input Stream Member Functions
.get ( ) ;
Example:
char ch ;
ch = cin.get ( ) ; // gets one character from keyboard
// & assigns it to the variable "ch"
.get (character) ;
Example:
char ch ;
cin.get (ch) ;
Winter Quarter
// gets one character from
// keyboard & assigns to "ch"
The Ohio State University
Gateway Engineering Education Coalition
Lect 27
P. 17
Engineering H192 - Computer Programming
More Input Stream Member Functions
.get (array_name, max_size) ;
Example:
char name[40] ;
cin.get (name, 40) ;
// Gets up to 39 characters
// and inserts a null at the end of the
// string "name". If a delimiter is
// found, the read terminates. The
// delimiter is not stored in the array,
// but it is left in the stream.
Winter Quarter
The Ohio State University
Gateway Engineering Education Coalition
Lect 27
P. 18
Engineering H192 - Computer Programming
More Input Stream Member Functions
.getline (array_name, max_size) ;
Example:
char name[40] ;
cin.getline (name, 40) ; // Gets up to 39 characters
// and assigns the string to "name". A
// null is inserted at the end of the string.
// Note that if a delimiter is found,
// it is removed from the stream, but it is
// not stored in the character array.
Winter Quarter
The Ohio State University
Gateway Engineering Education Coalition
Lect 27
P. 19
Engineering H192 - Computer Programming
More Input Stream Member Functions
.ignore ( ) ;
Ex:
cin.ignore ( ) ;
// gets and discards 1 character
cin.ignore (2) ; // gets and discards 2 characters
cin.ignore (80, '\n') ;
Winter Quarter
// gets and discards up to 80
// characters or until "newline"
// character, whichever comes
// first
The Ohio State University
Gateway Engineering Education Coalition
Lect 27
P. 20
Engineering H192 - Computer Programming
More Input Stream Member Functions
.peek( ) ;
Ex:
char ch ;
ch = cin.peek ( ) ; // peek at (don't take) character
.putback( ) ;
Ex:
char ch;
cin.putback (ch) ; // put character back in stream
Winter Quarter
The Ohio State University
Gateway Engineering Education Coalition
Lect 27
P. 21
Engineering H192 - Computer Programming
More I/O Stream Member Functions
.read( ) ; .write( ) ;
Ex:
char gross[144] ;
cin.read(gross,144) ; // reads 144 characters from
// input stream. Does NOT
// append '\0'
Winter Quarter
The Ohio State University
Gateway Engineering Education Coalition
Lect 27
P. 22
Engineering H192 - Computer Programming
File I/O with C++
#include <fstream>
using namespace std;
int main ( ) {
int a, b, c ;
ifstream fin ;
fin.open ( "my_input.dat") ;
fin >> a >> b ;
c=a+b;
ofstream fout ;
fout.open ( "my_output.dat");
fout << c << endl ;
fin.close ( ) ;
fout.close ( ) ;
}
Winter Quarter
//Create file input stream object
//Open input file
//Read two values from input file
//Create file output stream object
//Open output file
//Write result to output file
//Close input file
//Close output file
The Ohio State University
Gateway Engineering Education Coalition
Lect 27
P. 23