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







Share it Please
asad

About Author!

Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.

0 comments:

Post a Comment