How to Read a File Using ifstream in C++? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 2 Likes Like Report In C++, we can read the contents of the file by using the ifstream object to that file. ifstream stands for input file stream which is a class that provides the facility to create an input from to some particular file. In this article, we will learn how to read a file line by line through the ifstream class in C++. Read File Using ifstream in C++To read a file line by line using the ifstream class, we can use the std::getline() function. The getline() function can take input from any available stream so we can use the ifstream to the file with the getline() function. AlgorithmWe will first create an ifstream object associated with the given file.We then use the getline() function and pass the previously created ifstream object to it along with the buffer.In the following example, we will read a text file abc.txt line by line using the getline function of the input stream. abc.txtC++ Program to Read File using ifstream in C++ C++ // C++ Program to Read a File Line by Line using ifstream #include <fstream> #include <iostream> #include <string> using namespace std; int main() { // Open the file "abc.txt" for reading ifstream inputFile("abc.txt"); // Variable to store each line from the file string line; // Read each line from the file and print it while (getline(inputFile, line)) { // Process each line as needed cout << line << endl; } // Always close the file when done inputFile.close(); return 0; } Output Output after reading the text file abc.txt Time Complexity: O(N), where N is the number of characters in the fileAuxilary space: O(M), where M is the length of the longest line in the file. Create Quiz Comment G gaurav472 Follow 2 Improve G gaurav472 Follow 2 Improve Article Tags : C++ Programs C++ cpp-input-output cpp-file-handling CPP Examples +1 More Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like