Writing to a Text File
For file output, C++ uses analogs to cout
. So to prepare for file output, let’s review some basic facts about using cout
for console output:
• You must include the iostream
header file.
• The iostream
header file defines an ostream
class for handling output.
• The iostream
header file declares an ostream
variable, or object, called cout
.
• You must account for the std
namespace; for example, you can use the using
directive or the std::
prefix for elements such as cout
and endl
.
• You can use cout
with the <<
operator to read a variety of data types.
File output parallels this very closely:
• You must include the fstream
header file.
• The fstream
header file defines an ofstream
class for handling output.
• You need to declare ...
Get C++ Primer Plus 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.