CS 211:
Introduction to Computer Programming II
Instructor: Brian M. Dennis
Teaching Assistants:
Tom Lechner, Bin Lin, Rachel Goldsborough
http://www.cs.northwestern.edu/~bmd/cs211/

Streams
Already using them
cout << "foo";
cin >> some_int;
Today, more sophisticated uses
Participate in a hierarchy of classes
Forward reference inheritance

Stream Hierarchy

Stream member functions
ostream
put, takes a char, returns ostream ref
istream
get, no args returns next char
get, char ref stores next char
get, char*, size, delimeter
up to terminator or buf size
getline, no args,

Stream member functions
istream cont
ignore
putback
peek

Stream member functions
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main() {
const int SIZE = 80;
char buf1[SIZE];
char buf2[SIZE];
cout << "Enter a sentence:" << endl;
cin >> buf1;
cout << "\nThe string read with cin was:" << endl
<< buf1 << endl << endl;

Stream member functions
cin.get(buf2, SIZE);
cout << "The string read with cin.get was:" << endl
<< buf2 << endl;
cin.getline(buf2, SIZE);
cout << "\nThe sentence read with getline is:" << endl
<< buf2 << endl;
return 0;
}

Unformatted I/O
output
write, member function
cout.write(buf);
cout.write(buf, size)
input
read, member function
cout.read(buf, sz);

Stream manipulators
Integer output base
dec, oct, hex manip
setbase member function
Floating point precision
# of right digits displayed
setprecision manip
precision member function
Field manipulations
width member func, setw manip
skipws, noskipws
left, right
showbase, noshowbase
showpoint, noshowpoint
scientific, fixed

Stream flags
cin.eof()
Any input left?
cin.fail()
Error on input
cin.bad()
major error
cin.good()
bad, fail, eof all off
cout.rdstate()
Get flags as an integer
cin.clear()
reset flags
operator!
test badbit, failbit

That’s a Wrap
Reading
12.1 – 12.7