Showing posts with label Read Data From File. Show all posts
Showing posts with label Read Data From File. Show all posts

Friday, 25 July 2014

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

Structure of the Problem Requirements 

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
Compile the Programmed

C++ Filing Programming
Written in File