Chapter 2. Writing with Output Streams

In This Chapter

  • Using the insertion operator

  • Working with manipulators

  • Formatting your output

  • Using flags to format your output

  • Specifying a precision for writing numbers

  • Setting field widths

Years ago, one of us had an old computer that had 3000 bytes of memory. (Yes, that's three thousand bytes, not 3MB.) As an option, this computer came with a floppy disk drive that sat outside it. It did not have a hard drive. Therefore, if you didn't have a disk drive but you wanted to use a program, you had to type it! Ah, those were the days.

Nowadays, the notion of a computer without a hard drive seems almost unthinkable. Not only do your programs sit on the hard drive in the form of files, but your programs also create files to store on the hard drive.

When you use a word processor, you save your documents to a file. Imagine if every time you needed the same document, you had to retype it. In this chapter, we show you the different ways you can write to a file.

Inserting with the << Operator

Writing to a file is easy in C++. You're probably already familiar with how you can write to the console by using the cout object, like this:

cout << "Hey, I'm on TV!" << endl;

Get C++ All-In-One For Dummies®, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.