C++ examples for File Stream:Text File
Write Text to a File
#include <iostream> #include <fstream> using namespace std; int main() {/*from w ww. ja v a2s. c o m*/ char filename[10 + 1]; cout << "Enter a file name and press ENTER: "; cin.getline(filename, 10); ofstream file_out(filename); if (!file_out) { cout << filename << " could not be opened."; cout << endl; return -1; } cout << filename << " was opened." << endl; file_out << "I read the" << endl; file_out << "news today," << endl; file_out << "ooh boy."; file_out.close(); return 0; }