CS253: Software Development with C++

Fall 2018

Input/Output

See this page as a slide show

CS253 I/O

I/O Class Hierarchy

				       ┌─────┐
				       │ ios │
				       └─────┘
					  △
					  │
			 ┌────────────────┴────────────────┐
			 │                                 │
		    ┌────┴────┐                       ┌────┴────┐
		    │ istream │                       │ ostream │
		    └─────────┘                       └─────────┘
			 △                                 △
			 │                                 │
		  ┌──────┴───────┐                  ┌──────┴───────┐
		  │              │                  │              │
	    ┌─────┴────┐ ┌───────┴───────┐  ┌───────┴───────┐ ┌────┴─────┐
	    │ ifstream │ │ istringstream │  │ ostringstream │ │ ofstream │
	    └──────────┘ └───────────────┘  └───────────────┘ └──────────┘

More

			   ┌─────┐
			   │ ios │
			   └─────┘
			      △
			      │
	     ┌────────────────┴────────────────┐
	     │                                 │
	┌────┴────┐                       ┌────┴────┐
	│ istream │                       │ ostream │
	└─────────┘                       └─────────┘
	     △                                 △
	     │                                 │
      ┌──────┴───────┐                  ┌──────┴───────┐
      │              │                  │              │
┌─────┴────┐ ┌───────┴───────┐  ┌───────┴───────┐ ┌────┴─────┐
│ ifstream │ │ istringstream │  │ ostringstream │ │ ofstream │
└──────────┘ └───────────────┘  └───────────────┘ └──────────┘

Methods & Operator Overloading

			   ┌─────┐
			   │ ios │
			   └─────┘
			      △
			      │
	     ┌────────────────┴────────────────┐
	     │                                 │
	┌────┴────┐                       ┌────┴────┐
	│ istream │                       │ ostream │
	└─────────┘                       └─────────┘
	     △                                 △
	     │                                 │
      ┌──────┴───────┐                  ┌──────┴───────┐
      │              │                  │              │
┌─────┴────┐ ┌───────┴───────┐  ┌───────┴───────┐ ┌────┴─────┐
│ ifstream │ │ istringstream │  │ ostringstream │ │ ofstream │
└──────────┘ └───────────────┘  └───────────────┘ └──────────┘

What Header Files Define What

<iostream> <sstream> <fstream> <ios> or <iostream> <iomanip>

cin cout cerr clog

istringstream ostringstream stringstream

ifstream ofstream fstream

These don’t take an argument:

[no]boolalpha [no]showbase [no]showpoint [no]showpos [no]skipws [no]uppercase left / right / internal dec / hex / oct fixed / scientific

These do take an argument

resetiosflags setiosflags setbase setfill setprecision setw (non-sticky)

As of C++ 2011, <iostream> includes <ios> (but not <iomanip>). Previously, it just seemed to always happen.

strange type?

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'

basic_ostream

Why!?

w-streams

wchar_t

char c = 'X';
wchar_t wc = L'⻥';
cout << sizeof(c) << '\n'
     << sizeof(wc) << '\n';
1
4

basic_whatever<type>

Unformatted output

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

Unformatted input

Similarly, there is unformatted input:

ifstream in("/etc/resolv.conf");
string s;
getline(in, s);
cout << "First line: " << s << '\n';
char c;
while (in.get(c)) {
    if (c == '.') c = '*';
    cout << c;
}
First line: search cs.colostate edu colostate.edu
nameserver 129*82*45*181
nameserver 129*82*103*78
nameserver 129*82*103*79

seek/tell

ofstream out("xyz");
out.write("Hxllo there\n", 11);
out.seekp(1);
out.put('e');
out.close();
cout << ifstream("xyz").rdbuf();
Hello there

Misuse of .open()

ifstream and ofstream both have .open() methods:

ifstream in;
in.open("/s/bach/a/class/cs253/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:

ifstream in("/s/bach/a/class/cs253/pub/ducks");
char c;
while (in.get(c))
    cout << c;
Huey (red)
Dewey (blue)
Louie (green)

Misuse of .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?

The right way to detect end-of-file

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

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-10-14T17:40

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building