Sunday, 3 March 2013

How read text from a text file using C++


Program:
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 
  getch();
  return 0;
}

Output:
Writing this to a file.

Thanks for watching...Comment it...

No comments:

Post a Comment