Using streams to output text - C++ File Stream

C++ examples for File Stream:Text File

Description

Using streams to output text

Demo Code

#include <cstdlib> 
 #include <iostream> 
 #include <string> 
 #include <fstream> 

 using namespace std; 

 int main(int argc, char *argv []) 
 { /*from  ww w.j a  v a 2  s  . co  m*/
     std::string textArray [] = 
     { 
        "test", 
        "is", 
        "a ", 
        " test", 
        "\"Inquiries.\" ", 
        "My test" 
   }; 

   cout << "Opening file..."; 
   std ::ofstream outputFile("Jeeves.txt"); 
   if (outputFile) 
   { 
       cout << "Done" << endl; 

       cout << endl << "W riting text to the file Jeeves. txt ..."; 
       int i = 0; 
       while (i<12) 
       { 
           outputFile << textArray [i] << endl; 
           i++; 
       }; 
       cout << "Done" << endl; 

       cout << endl << "Closing Jeeves.txt ..."; 
       outputFile.close(); 

       cout << "Done" << endl << endl; 
  } 
  else 
  { 
      cout << "Could not open file." << endl; 
  } 

  return 0; 
}

Result


Related Tutorials