Write Text to a File - C++ File Stream

C++ examples for File Stream:Text File

Description

Write Text to a File

Demo Code

#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;
}

Result


Related Tutorials