reading a text file : Text File « File « C++






reading a text file

  
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }else 
    cout << "Unable to open file"; 

  return 0;
}
  
    
  








Related examples in the same category

1.Use the getline function with the C++ string classUse the getline function with the C++ string class
2.Write strings to disk
3.Write string to a file
4.Write to file: ofstream
5.Read from file.Read from file.
6.Reading from a File
7.Writing to a File
8.writing on a text file
9.Sequential Files