1 / 6

We ’ ve Looked at Basic Input and Output Already

We ’ ve Looked at Basic Input and Output Already. How to move data into and out of a program Using argc and argv to pass command line args Using cout to print data out to the terminal Using cin to obtain data from the user at run-time Using an ifstream to read data in from a file

dylanc
Download Presentation

We ’ ve Looked at Basic Input and Output Already

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. We’ve Looked at Basic Input and Output Already • How to move data into and out of a program • Using argc and argv to pass command line args • Using cout to print data out to the terminal • Using cin to obtain data from the user at run-time • Using an ifstream to read data in from a file • Using an ofstream to write data out to a file • How to move data between strings, basic types • Using an istringstream to extract int values • Using an ostringstream to assemble a string • This module will expand on those ideas • Towards understanding better how steams work • Towards using them even more effectively

  2. Example: Moving Data Into and Out of a Struct // struct, operator declarations (in point2d.h) struct Point2D { Point2D (int x, int y); bool operator< (const Point2D &) const; int x_; int y_; }; // extraction operator(also in point2d.h) istream & operator>> (istream &, Point2D &); // insertion operator(also in point2d.h) ostream & operator<< (ostream &, const Point2D &);

  3. Defining Stream Operators for a Struct // operator definitions (in point2d.cpp) // extraction operator(also in point2d.h) istream & operator>> (istream &i, Point2D &p) { i >> p.x_ >> p.y_; // extract both variables return i; } // insertion operator(also in point2d.h) ostream & operator<< (ostream &o, const Point2D &p) { o << p.x_ << "" << p.y_; // space delimited return o; }

  4. // uses standard io streams #include <iostream> using namespace std; #include "point2d.h" int main(int, char*[]){ while (true) { Point2D p(0, 0); cout << "Please enter 2 coordinates (or hit Ctrl-C to quit): "; cin >> p; cout << endl << "You entered " << p << endl; } return 0; } Now Can Use the Struct with Various Streams // uses string streams #include <iostream> #include <string> #include <sstream> using namespace std; #include "point2d.h" int main(int, char*[]){ Point2D p(0, 0); string s ("64 81"); istringstreami(s); i >> p; ostringstream o; o << p; cout << "The string is " << o.str() << endl; return 0; }

  5. An output stream may hold onto data for a while, internally E.g., writing chunks of text rather than a character at a time is efficient When it writes data out (e.g., to a file, the terminal window, etc.) is entirely up to the stream, unless you tell it to flush out its buffers If a program crashes, any un-flushed stream data is lost Terminal output & files are as of last flush, not as of where it crashed So, flushing streams reasonably often is an excellent debugging trick Can tie an input stream directly to an output stream Output stream is then flushed by call to input stream extraction operator E.g., my_istream.tie(&my_ostream); cout is already tied to cin (useful for prompting the user, getting input) Also can flush streams directly using stream manipulators E.g., cout << flush; or cout << endl; or cout << unitbuf; Other stream manipulators are useful for formatting streams Field layout: setwidth, setprecision, etc. Display notation: oct, hex, dec, boolalpha, noboolalpha, scientific, etc. Flushing Streams (and Stream Manipulators)

  6. A Few More Useful Details • Cannot copy or assign stream objects • Copy construction or assignment syntax using them results in a compile-time error • Extraction operator consumes data from input stream • “Destructive read” that reads a different element each time • Use a variable if you want to read same value repeatedly • Need to test streams’ condition states • E.g., calling the is_open method on a file stream • E.g., use the stream object in a while or if test • Insertion and extraction operators return a reference to a stream object, so can test them too • File stream destructor calls close automatically

More Related