HOW TO READ DATA FROM A TEXT FILE IN C++

Files are use to store text data in C++. In this problem we learn how to read data from a text file and display that text on the console screen . Just follow the simple instruction and syntax of file handling.

Source Code



#include <iostream>
#include <fstream>                 // reading a text file
#include <string>

using namespace std;

int main () 
{
 string line;
 cout<<" \n\n\n ************ How To Read From File *************** \n\n\n ";
 
 ifstream myfile (" ReadFile.txt ");               //ifstream use for the retrival data from file

 if (myfile.is_open())       //Check if the File Open or is not
 {
  while (! myfile.eof() )      //Read the File till end the words
  {
   getline (myfile,line);     //getline is a function which is read line by line from file
   cout << line << endl;
  }

 myfile.close();         //After Reading the data file is going to close
 }

 else 
 {
  cout << " \n\n Unable to open file\n\n ";    
 }

return 0; }

Output of the Program


C++ Filing Programming



Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment