O'Reilly logo

C++ For Dummies®, 6th Edition by Stephen R. Davis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 23. Using Stream I/O

In This Chapter

  • Performing input/output

  • Rediscovering stream I/O as an overloaded operator

  • Examining the other methods of the file class

  • Using stream buffer I/O

Programs appearing before this chapter read from the cin input object and output through the cout output object. Perhaps you haven't really thought about it much, but this input/output technique is a subset of what is known as stream I/O.

In this chapter, I describe stream I/O in more detail. I must warn you that stream I/O is too large a topic to be covered completely in a single chapter — entire books are devoted to this one topic. Fortunately for both of us, there isn't all that much that you need to know about stream I/O to write the vast majority of programs.

How Stream I/O Works

Stream I/O is based on overloaded versions of operator>>() and operator <<(). The declaration of these overloaded operators is found in the include file iostream, which are included in all the programs beginning in Chapter 1. The code for these functions is included in the standard library, which your C++ program links with.

The following code shows just a few of the prototypes appearing in iostream:

//for input we have: istream& operator>>(istream& source, char *pDest); istream& operator>>(istream& source, string &sDest); istream& operator>>(istream& source, int &dest); istream& operator>>(istream& source, double &dest); //...and so forth... //for output we have: ostream& operator<<(ostream& dest, char *pSource); ostream& ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required