Reading from a File using ifstream class




This video tutorial explains how to read from a file using an ifstream class object.
You are gonna learn how to create an object of ifstream class, how to open a file for reading, how to associate a file with an object of ifstream class, how to read from the file using getline function in detail with example.

source code for this tutorial

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

int main()
{
ifstream file;
file.open("anil.txt");
if(!file.is_open()){
    cout << "error while opening the file";

}else{
cout << "reading from the file contents are"<<endl;
string line;
while(file.good()){
getline(file,line);
cout << line <<endl;
}
}

    return 0;
}

 
Back To Top