C++ ofstream write text to a file

Description

C++ ofstream write text to a file

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main()/* w w  w . j a  v a 2  s.  co m*/
{
   string fname = "list.dat";  // the file you are working with
   void inOut(ofstream&);      // function prototype
   ofstream outFile;
   outFile.open(fname.c_str());
   if (outFile.fail())   // check for a successful open
   {
      cout << "\nThe output file " << fname << " was not successfully opened" << endl;
      exit(1);
   }
   inOut(outFile);  // call the function
   return 0;
}
void inOut(ofstream& fileOut)
{
   const int NUMLINES = 5;  // number of lines of text
   string line;
   int count;
   cout << "Please enter five lines of text:" << endl;
   for (count = 0; count < NUMLINES; count++)
   {
      getline(cin,line);
      fileOut << line << endl;
   }
   cout << "\nThe file has been successfully written." << endl;
   return;
}



PreviousNext

Related