How to Read a File Using ifstream in C++? Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article 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. Comment More infoAdvertise with us Next Article How to Read a File Using ifstream in C++? gaurav472 Follow Improve Article Tags : C++ Programs C++ cpp-input-output cpp-file-handling CPP Examples +1 More Practice Tags : CPP Similar Reads How to Read From a File in C++? Reading from a file means retrieving the data stored inside a file. C++ file handling allows us to read different files from our C programs. This data can be taken as input and stored in the program for processing. Generally, files can be classified in two types:Text File: Files that contains data i 4 min read How to Read a File Line by Line in C++? In C++, we can read the data of the file for different purposes such as processing text-based data, configuration files, or log files. In this article, we'll learn how to read a file line by line in C++. Read a File Line by Line in C++We can use the std::getline() function to read the input line by 2 min read How to Read File into String in C++? In C++, file handling allows us to read and write data to an external file from our program. In this article, we will learn how to read a file into a string in C++. Reading Whole File into a C++ StringTo read an entire file line by line and save it into std::string, we can use std::ifstream class to 2 min read How to Read Input Until EOF in C++? In C++, EOF stands for End Of File, and reading till EOF (end of file) means reading input until it reaches the end i.e. end of file. In this article, we will discuss how to read the input till the EOF in C++. Read File Till EOF in C++The getline() function can be used to read a line in C++. We can 2 min read How to Get Error Message When ifstream Open Fails in C++? In C++, the std::ifstream class is used to open a file for input operations. It associates an input stream to the file. However, due to some reasons, it may be unable to open the requested file. In this article, we will learn how to show an error message when the ifstream class fails to open the fil 2 min read How to Use cin.fail() Method in C++? In C++, the cin.fail() method is a part of <iostream> library that is used to check whether the previous input operation has succeeded or not by validating the user input. In this article, we will learn how to use cin.fail() method in C++. Example: Input: Enter an integer: aOutput: Invalid Inp 2 min read How to Use the ignore() Function in C++? In C++, the ignore() function is a part of std::basic_istream that is used to discard the characters in the stream until the given delimiter(including it) is reached and then extracts the left-out remainder. In this article, we will learn how to use the ignore() function in C++. C++ ignore() Functio 2 min read C++ Program to Make a File Read-Only Here, we will build C++ Program to Make a File Read-Only using 2 approaches i.e. Using ifstreamUsing fstreamC++ programming language offers a library called fstream consisting of different kinds of classes to handle the files while working on them. The classes present in fstream are ofstream, ifstre 2 min read How to Read and Write Arrays to/from Files in C++? In C++, an array is a collection of elements of the same type placed in contiguous memory locations. A file is a container in a computer system for storing information. In this article, we will learn how to read and write arrays to/from files in C++. Example: Input: Array = {1, 2, 3, 4, 5}Output: // 2 min read How Can I Get a File Size in C++? In C++, we often need to determine the size of a file which is the number of bytes in a file. This is done for various applications, such as file processing or validation. In this article, we will learn how to get the file size in C++. Example: Input: FilePath = "C:/Users/Desktop/myFile.txt" Output: 2 min read Like