Streams Code

Download Report

Transcript Streams Code

C++ Streams
C++ Programming Certificate
University of Washington
Cliff Green
© Bruce M. Reynolds &
Cliff Green, 2002
1
C++ Streams
// stream output, Quincy Ex. 2.17
#include <iostream>
using std::cout;
int main() {
int amt (3);
char ch ('A‘);
double val (1.23);
// --- display the initialized variables
cout << amt << ' ' << ch << ' ' << val;
return 0;
}
© Bruce M. Reynolds &
Cliff Green, 2002
2
C++ Streams
// formatted stream output, Quincy Ex. 2.18
#include <iostream>
using std::cout; using std::dec; using std::oct;
using std::hex;
int main() {
int amount = 123;
cout << dec << amount << ' ' << oct << amount << ' '
<< hex << amount;
return 0;
}
© Bruce M. Reynolds &
Cliff Green, 2002
3
C++ Streams
// reading input, Quincy Ex. 2.19
#include <iostream>
using std::cout; using std::cin;
int main() {
short int amount;
cout << "Enter an amount...";
cin >> amount;
cout << "The amount you entered was " << amount;
return 0;
}
© Bruce M. Reynolds &
Cliff Green, 2002
4
C++ Streams
// reading a string, Quincy Ex. 2.20
#include <iostream>
#include <string>
using std::cout; using std::cin;
using std::string;
int main() {
cout << "Enter a name...";
char name[20];
cin >> name;
cout << "The name you entered was " << name << ‘\n’;
cout << “Enter a city: “;
string city;
cin >> city;
cout << “The city you entered was “ << city;
return 0;
}
© Bruce M. Reynolds &
Cliff Green, 2002
5
C++ Streams
// setw manipulator, Quincy Ex. 16.3
#include <iostream>
#include <iomanip>
using std::cout; using std::endl;
using std::setw;
int main() {
static double values[] =
{ 1.23, 35.36, 653.7, 4358.224 };
static char const* names[] =
{"Zoot", "Jimmy", "Al", "Stan"};
for (int i = 0; i < 4; ++i)
cout << setw(6) << names[i]
<< setw(10) << values[i] << endl;
return 0;
}
© Bruce M. Reynolds &
Cliff Green, 2002
6
C++ Streams
// fill and width member functions, Quincy Ex. 16.4
#include <iostream>
using std::cout; using std::endl;
int main() {
static double values[] =
{ 1.23, 35.36, 653.7, 4358.224 };
for (int i = 0; i < 4; ++i) {
cout.width(10);
cout.fill('*');
cout << values[i] << endl;
}
return 0;
}
© Bruce M. Reynolds &
Cliff Green, 2002
7
C++ Streams
// the setiosflags manipulator, Quincy Ex. 16.5
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
static double values[] =
{ 1.23, 35.36, 653.7, 4358.224 };
static char const* names[] =
{"Zoot", "Jimmy", "Al", "Stan"};
for (int i = 0; i < 4; ++i) {
cout << setiosflags(ios::left)
<< setw(6) << names[i]
<< resetiosflags(ios::left)
<< setiosflags(ios::right)
<< setw(10) << values[i] << endl;
}
return 0;
}
© Bruce M. Reynolds &
Cliff Green, 2002
8
C++ Streams
// istream get member function, Quincy Ex. 16.9
#include <iostream>
using namespace std;
int main() {
char line[25], ch = 0, *cp;
cout << " Type a line terminated by 'x'" << endl << '>';
cp = line;
while (ch != 'x') {
cin >> ch;
*cp++ = ch;
}
*cp = '\0';
cout << ' ' << line;
cout << "\n Type another one" << endl << '>';
cp = line;
ch = 0;
while (ch != 'x') {
cin.get(ch);
*cp++ = ch;
}
*cp = '\0';
cout << ' ' << line;
return 0;
}
© Bruce M. Reynolds &
Cliff Green, 2002
9
C++ Streams
// istream getline member function, Quincy Ex. 16.11
#include <iostream>
using std::cout; using std::cin; using std::endl;
int main() {
char line[25];
cout << " Type a line terminated by 'q'"
<< endl << '>';
cin.getline(line, 25, 'q');
cout << ' ' << line;
return 0;
}
© Bruce M. Reynolds &
Cliff Green, 2002
10
C++ Streams
// istream read member function, Quincy Ex. 16.12
#include <iostream>
using std::cout; using std::cin;
int main() {
char msg[23];
cin.read(msg, sizeof msg);
cout << msg;
return 0;
}
© Bruce M. Reynolds &
Cliff Green, 2002
11
C++ Streams
// file appending stream output, Quincy Ex. 16.14
#include <fstream>
using std::ofstream;
int main() {
ofstream tfile("test.dat", ios::app);
tfile << ", and these are more";
return 0;
}
© Bruce M. Reynolds &
Cliff Green, 2002
12
C++ Streams
// reading a writing a stream file, Quincy Ex. 16.20
#include <fstream>
#include <cctype>
using namespace std;
int main() {
char const* fname = "test.dat";
// --- read the file into an array
ifstream tfile(fname, ios::in | ios::out | ios::binary);
ostream ofile(tfile.rdbuf());
char tdata[100];
int i = 0;
while (!tfile.eof() && i < sizeof tdata) {
tfile.get(tdata[i++]);
}
ofile.seekp(0, ios::end);
ofile << "\r\n";
for (int j = 0; i && j < i-1; ++j) {
ofile.put(static_cast<char>(toupper(tdata[j])));
}
return 0;
}
© Bruce M. Reynolds &
Cliff Green, 2002
13
C++ Streams
// extracting from istringstream, Quincy Ex. 16.24
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
string instr;
cout << "Enter an integer, a float, and a string: " << flush;
getline(cin, instr);
istringstream istr(instr);
int n;
float f;
string s;
istr >> n >> f >> s;
cout << "Extracted from istringstream: "
<< n << ' ' << f << ' ' << s << endl;
return 0;
}
© Bruce M. Reynolds &
Cliff Green, 2002
14
C++ Streams
// the ostringstream class, Quincy Ex. 16.26
#include “Date.h”
#include <iostream>
#include <sstream>
using std::ostringstream; using std::cout; using std::endl;
int main() {
ostringstream ostr;
Date dt(11,17,1997);
ostr << "Judy's next birthday is ";
ostr << dt;
ostr << ". Let's have a party.";
ostr << endl;
// --- now display the ostrstream object
cout << ostr.str();
return 0;
}
© Bruce M. Reynolds &
Cliff Green, 2002
15
C++ Streams
// using ostream>> for debugging
// includes, include guard, using declarations left out
class Wheel { // traditional technique
private:
friend ostream& operator<<(ostream &, Wheel const &);
public:
Wheel() : radius(15), allSeason(true) {}
void clearAllSeason() { allSeason = false; }
private:
int radius;
bool allSeason;
};
ostream& operator<< (ostream&, Wheel const&);
class Car { // alternative technique, friends not needed
public:
Car() : velocity(0) { spare.clearAllSeason(); }
void go() { velocity = 60; }
ostream& streamOut(ostream&) const;
private:
enum { NWHEELS = 4 };
Wheel wheel[NWHEELS];
Wheel spare;
int velocity;
};
ostream& operator<<(ostream &, Car const & );
© Bruce M. Reynolds &
Cliff Green, 2002
16
C++ Streams
// using ostream>> for debugging
ostream& operator<<(ostream& os, Wheel const & rhs) {
os << "Radius is " << rhs.radius;
if ( rhs.allSeason ) {
os << " (AllSeason)";
}
os << endl;
return os;
}
ostream& operator<<(ostream& os, Car const & rhs) {
return rhs.streamOut(os);
}
ostream& Car::streamOut(ostream& os) const {
for( int wdx=0; wdx < NWHEELS; ++wdx) {
os << ”//Wheel " << wdx << ": " << wheel[wdx];
}
os << ”//Spare: " << spare;
os << ”//Velocity is: " << velocity << endl;
return os;
}
© Bruce M. Reynolds &
Cliff Green, 2002
17
C++ Streams
// using ostream>> for debugging
int main() {
Car car;
car.go();
cout << car;
return 0;
}
//Wheel 0: Radius
//Wheel 1: Radius
//Wheel 2: Radius
//Wheel 3: Radius
//Spare: Radius is
//Velocity is: 60
is 15
is 15
is 15
is 15
15
© Bruce M. Reynolds &
Cliff Green, 2002
(AllSeason)
(AllSeason)
(AllSeason)
(AllSeason)
18
C++ Streams
// forget atoi(), itoa(), etc.
#include <iostream>
#include <sstream>
using std::istringstream; using std::ostringstream;
using std::cout;
int main() {
char const * pstr = ”3.14159";
istringstream iss( pstr );
double pi;
iss >> pi;
ostringstream oss;
oss << "Pi is: " << pi;
cout << oss.str();
return 0;
}
© Bruce M. Reynolds &
Cliff Green, 2002
19