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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access