Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Streams © Bruce M. Reynolds & Cliff Green, 2002 1 C++ Programming Certificate University of Washington Cliff Green.

Similar presentations


Presentation on theme: "C++ Streams © Bruce M. Reynolds & Cliff Green, 2002 1 C++ Programming Certificate University of Washington Cliff Green."— Presentation transcript:

1 C++ Streams © Bruce M. Reynolds & Cliff Green, 2002 1 C++ Programming Certificate University of Washington Cliff Green

2 C++ Streams © Bruce M. Reynolds & Cliff Green, 2002 2 // stream output, Quincy Ex. 2.17 #include 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; }

3 C++ Streams © Bruce M. Reynolds & Cliff Green, 2002 3 // formatted stream output, Quincy Ex. 2.18 #include 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; }

4 C++ Streams © Bruce M. Reynolds & Cliff Green, 2002 4 // reading input, Quincy Ex. 2.19 #include 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; }

5 C++ Streams © Bruce M. Reynolds & Cliff Green, 2002 5 // reading a string, Quincy Ex. 2.20 #include 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; }

6 C++ Streams © Bruce M. Reynolds & Cliff Green, 2002 6 // setw manipulator, Quincy Ex. 16.3 #include 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; }

7 C++ Streams © Bruce M. Reynolds & Cliff Green, 2002 7 // fill and width member functions, Quincy Ex. 16.4 #include 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; }

8 C++ Streams © Bruce M. Reynolds & Cliff Green, 2002 8 // the setiosflags manipulator, Quincy Ex. 16.5 #include 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; }

9 C++ Streams © Bruce M. Reynolds & Cliff Green, 2002 9 // istream get member function, Quincy Ex. 16.9 #include using namespace std; int main() { char line[25], ch = 0, *cp; cout '; cp = line; while (ch != 'x') { cin >> ch; *cp++ = ch; } *cp = '\0'; cout << ' ' << line; cout '; cp = line; ch = 0; while (ch != 'x') { cin.get(ch); *cp++ = ch; } *cp = '\0'; cout << ' ' << line; return 0; }

10 C++ Streams © Bruce M. Reynolds & Cliff Green, 2002 10 // istream getline member function, Quincy Ex. 16.11 #include using std::cout; using std::cin; using std::endl; int main() { char line[25]; cout << " Type a line terminated by 'q'" '; cin.getline(line, 25, 'q'); cout << ' ' << line; return 0; }

11 C++ Streams © Bruce M. Reynolds & Cliff Green, 2002 11 // istream read member function, Quincy Ex. 16.12 #include using std::cout; using std::cin; int main() { char msg[23]; cin.read(msg, sizeof msg); cout << msg; return 0; }

12 C++ Streams © Bruce M. Reynolds & Cliff Green, 2002 12 // file appending stream output, Quincy Ex. 16.14 #include using std::ofstream; int main() { ofstream tfile("test.dat", ios::app); tfile << ", and these are more"; return 0; }

13 C++ Streams © Bruce M. Reynolds & Cliff Green, 2002 13 // reading a writing a stream file, Quincy Ex. 16.20 #include 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 (toupper(tdata[j]))); } return 0; }

14 C++ Streams © Bruce M. Reynolds & Cliff Green, 2002 14 // extracting from istringstream, Quincy Ex. 16.24 #include 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; }

15 C++ Streams © Bruce M. Reynolds & Cliff Green, 2002 15 // the ostringstream class, Quincy Ex. 16.26 #include “Date.h” #include 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; }

16 C++ Streams © Bruce M. Reynolds & Cliff Green, 2002 16 // 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 & );

17 C++ Streams © Bruce M. Reynolds & Cliff Green, 2002 17 // 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; }

18 C++ Streams © Bruce M. Reynolds & Cliff Green, 2002 18 // using ostream>> for debugging int main() { Car car; car.go(); cout << car; return 0; } //Wheel 0: Radius is 15 (AllSeason) //Wheel 1: Radius is 15 (AllSeason) //Wheel 2: Radius is 15 (AllSeason) //Wheel 3: Radius is 15 (AllSeason) //Spare: Radius is 15 //Velocity is: 60

19 C++ Streams © Bruce M. Reynolds & Cliff Green, 2002 19 // forget atoi(), itoa(), etc. #include 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; }


Download ppt "C++ Streams © Bruce M. Reynolds & Cliff Green, 2002 1 C++ Programming Certificate University of Washington Cliff Green."

Similar presentations


Ads by Google