Show Lecture.IO as a slide show.
┌─────┐ │ ios │ └─────┘ △ │ ┌────────────────┴────────────────┐ │ │ ┌────┴────┐ ┌────┴────┐ │ istream │ │ ostream │ └─────────┘ └─────────┘ △ △ │ │ ┌──────┴───────┐ ┌──────┴───────┐ │ │ │ │ ┌─────┴────┐ ┌───────┴───────┐ ┌───────┴───────┐ ┌────┴─────┐ │ ifstream │ │ istringstream │ │ ostringstream │ │ ofstream │ └──────────┘ └───────────────┘ └───────────────┘ └──────────┘
┌─────┐ │ ios │ └─────┘ △ │ ┌────────────────┴────────────────┐ │ │ ┌────┴────┐ ┌────┴────┐ │ istream │ │ ostream │ └─────────┘ └─────────┘ △ △ │ │ ┌──────┴───────┐ ┌──────┴───────┐ │ │ │ │ ┌─────┴────┐ ┌───────┴───────┐ ┌───────┴───────┐ ┌────┴─────┐ │ ifstream │ │ istringstream │ │ ostringstream │ │ ofstream │ └──────────┘ └───────────────┘ └───────────────┘ └──────────┘
ios
(input/output stream) is the base class
istream
and ostream
are subclasses of ios
istream
is-a ios
ostream
is-a ios
ifstream
and istringstream
are subclasses of istream
ifstream
is-a istream
istringstream
is-a istream
ofstream
and ostringstream
are subclasses of ostream
ofstream
is-a ostream
ostringstream
is-a ostream
┌─────┐ │ ios │ └─────┘ △ │ ┌────────────────┴────────────────┐ │ │ ┌────┴────┐ ┌────┴────┐ │ istream │ │ ostream │ └─────────┘ └─────────┘ △ △ │ │ ┌──────┴───────┐ ┌──────┴───────┐ │ │ │ │ ┌─────┴────┐ ┌───────┴───────┐ ┌───────┴───────┐ ┌────┴─────┐ │ ifstream │ │ istringstream │ │ ostringstream │ │ ofstream │ └──────────┘ └───────────────┘ └───────────────┘ └──────────┘
operator<<
part of?
operator>>
part of?
.open()
part of?
.get()
part of?
<sstream>
| <fstream>
| <iostream>
| <iomanip>
| |
---|---|---|---|---|
objects | manipulators | |||
These don’t take an argument:
boolalpha noboolalpha showbase noshowbase showpoint noshowpoint showpos noshowpos skipws noskipws uppercase nouppercase left right internal dec hex oct fixed scientific | These do take an argument
resetiosflags setiosflags setbase setfill setprecision setw (non-sticky) |
Consider the error message from this bad code:
cout.zork();
c.cc:1: error: 'std::ostream' {aka 'class std::basic_ostream<char>'} has no member named 'zork'
std::basic_ostream<char>
” business?
cout
an object of type ostream
?
cout
an object of type ostream
?
cout
is an object of type basic_ostream<char>
.
typedef basic_ostream<char> ostream;
typedef basic_istream<char> istream;
typedef basic_string<char> string;
Why!?
string
is a sequence of char
values; wstring
is a sequence of wchar_t
values.
ostream
is an output stream of char
values; wostream
is an output stream of wchar_t
values.
istream
is an input stream of char
values; wistream
is an input stream of wchar_t
values.
wchar_t
wchar_t
? It’s a wide character.
wchar_t
is a fundamental type,
like char
or int
.
char
.
char c = 'X'; wchar_t wc = L'⻥'; cout << sizeof(c) << '\n' << sizeof(wc) << '\n';
1 4
basic_whatever<type>
ostream
, then copied to ostream
code
to a new wostream
class, changing char
to wchar_t
.
ostream
but not in wostream
.
basic_ostream
, which takes either
char
or wchar_t
as a template argument.
ostream
and string
.
We’re familiar with formatted I/O using <<
or >>
:
cout << "π ≅ " << 355/113.0 << endl;
π ≅ 3.14159
There’s also unformatted output:
cout.put('h'); cout.put('i'); char data[] = " there\nextra"; cout.write(data, 7);
hi there
Similarly, there is unformatted input:
ifstream in("/etc/resolv.conf"); string s; getline(in, s); cout << "First: " << s << '\n'; char c; while (in.get(c)) { if (c == '.') c = '*'; cout << c; }
First: search cs.colostate edu colostate.edu nameserver 129*82*45*181 nameserver 129*82*103*78 nameserver 129*82*103*79
.unget()
undoes the most recent .get()
.
.peek()
is sort of like .get()
followed by .unget()
.
.ignore()
reads until a delimiter.
ostream
has a put pointer, an integer value that keeps track
of how far along in the output we are.
istream
has a get pointer.
ofstream out("xyz"); out.write("Hxllo there\n", 11); out.seekp(1); out.put('e'); out.close(); cout << ifstream("xyz").rdbuf();
Hello there
.open()
ifstream
and ofstream
both have .open()
methods:
ifstream in; const string home = getpwnam("cs253")->pw_dir; in.open(home+"/pub/ducks"); char c; while (in.get(c)) cout << c;
Huey (red) Dewey (blue) Louie (green)
Why have that extra step? Just associate the filename at object construction:
const string home = getpwnam("cs253")->pw_dir; ifstream in(home+"/pub/ducks"); char c; while (in.get(c)) cout << c;
Huey (red) Dewey (blue) Louie (green)
.eof()
.eof()
.
ifstream in("/etc/resolv.conf"); string line; while (!in.eof()) { getline(in, line); cout << line << '\n'; }
search cs.colostate edu colostate.edu nameserver 129.82.45.181 nameserver 129.82.103.78 nameserver 129.82.103.79
Where did that extra line come from?
Don’t inquire in advance—just go ahead and read. It will succeed or fail:
ifstream in("/etc/resolv.conf"); string line; while (getline(in, line)) cout << line << '\n';
search cs.colostate edu colostate.edu nameserver 129.82.45.181 nameserver 129.82.103.78 nameserver 129.82.103.79