FoodForFutureGeeks

Friday 29 June 2012

Program to Read a file line by line in C++


#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string strfile="filenamewithabsolutepath.txt";
 ifstream ifptr;
 ifptr.open(strfile);
if(!ifptr.is_open())
{
 cout<<"Unable to open file\n";
 }
 else{
 //read the file till the end
    while(!ifptr.eof())
    {
     string line;
     getline(ifptr,line);
     cout<<"line:"<<line<<"\n";
    }//end of while
   }//end of else
   return 0;
 }//end of main

No comments:

Post a Comment