1 / 18

Passing Streams to Functions

Passing Streams to Functions. Passing Streams to Functions. One Rule: always pass a stream as a reference. file: fileopen.h // Pre: template parameter T must be either ifstream or ofstream type. template < typename T>

lyre
Download Presentation

Passing Streams to Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Passing Streams to Functions

  2. Passing Streams to Functions • One Rule: always pass a stream as a • reference

  3. file: fileopen.h // Pre: template parameter T must be either ifstream or ofstream type. template <typename T> void fileopen (T & filestr, const string promptpart) { const int MAX_TRIES = 5; int count = 0;     string filename; cout<<”enter name of “<<promptpart<<” file: “; cin>>filename; filestr.open(filename.c_str());     while (!filestr)     { filestr.clear(); // may be necessary on your platform cout<<”ERROR: file not connected. Try again...”<<endl; cout<<”enter name of “<<promptpart<<” file: “; cin>>filename; filestr.open(filename.c_str());         count++;         if (count > MAX_TRIES)         { cout<<”NOT CONNECTING AFTER “<<MAX_TRIES<<” ATTEMPTS...BAILING OUT” <<”...”<<endl;            exit(1);         }     } return; }

  4. #include <fstream> #include “fileopen.h” int main() { ifstream in; fileopen(in, “input”);

  5. Operator Overloading ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; }

  6. Operator Overloading ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; } // example 1 point p1, p2; cout << p1; cout << p1 << " “ << p2;

  7. Operator Overloading ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; } // example 1 point p1, p2; cout<< p1; cout << p1 << “ ” << p2; // example 2 ofstreamfout; fout << p1; fout << p1 << “ ” << p2;

  8. Chaining Stream Processing... // example 1 cout << p1 << “ ” << p2; ... ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; }

  9. Chaining Stream Processing... cout << p1 // example 1 cout << p1 << “ ” << p2; ... ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; }

  10. Chaining Stream Processing... cout << p1 executes overloaded operator<< // example 1 cout << p1 << “ ” << p2; ... ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; }

  11. Chaining Stream Processing... cout << p1 executes overloaded operator<< returns cout with the points data added to the stream // example 1 cout << p1 << “ ” << p2; ... ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; }

  12. Chaining Stream Processing... cout << p1 executes overloaded operator<< returns cout with the points data added to the stream cout << “ ” // example 1 cout << p1 << “ ” << p2; ... ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; }

  13. Chaining System Processing... cout << p1 executes overloaded operator<< returns cout with the points data added to the stream cout << “ ” returns cout with the space added to the stream // example 1 cout << p1 << “ ” << p2; ... ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; }

  14. Chaining Stream Processing... cout << p1 executes overloaded operator<< returns cout with the points data added to the stream cout << “ ” returns cout with the space added to the stream cout << p2 // example 1 cout << p1 << “ ” << p2; ... ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; }

  15. Chaining Stream Processing... cout << p1 executes overloaded operator<< returns cout with the points data added to the stream cout << “ ” returns cout with the space added to the stream cout << p2 executes overloaded operator<< // example 1 cout << p1 << “ ” << p2; ... ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; }

  16. Chaining Stream Processing... cout << p1 executes overloaded operator<< returns cout with the points data added to the stream cout << “ ” returns cout with the space added to the stream cout << p2 executes overloaded operator<< returns cout with the points data added to the stream // example 1 cout << p1 << “ ” << p2; ... ostream& operator << (ostream & out, const point & p) { out << “(” << p.m_X << “, ” << p.m_Y << “)”; return out; }

  17. Final Note iostream and fstream are of the same family getline, ignore, get, putback, etc are all available for filestreams as well! ifstream fin; char input; fin.open(“input.dat”); while (in.get(input)) { process_data(input);

  18. End of Session

More Related