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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.