How to Read Input Until EOF in C++? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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 use this function to read the entire file until EOF by using it with a while loop. This method reads the file line by line till the EOF is reached. C++ Program to Read Input Until EOF C++ // C++ program to use getline() to read input until EOF is // reached. #include <fstream> #include <iostream> #include <string> using namespace std; int main() { // Specify filename to read (replace with your actual // file) string filename = "input.txt"; // Open file for reading ifstream inputFile(filename); // Check file open success if (!inputFile.is_open()) { cerr << "Error opening file: " << filename << endl; return 1; } // String to store each line string line; // Read each line until EOF cout << "The file contents are: \n"; while (getline(inputFile, line)) { // Process each line (replace with your logic) cout << line << endl; } // Close file when done inputFile.close(); return 0; } Output The file contents are: This file contains some random text that can be scanned by a C++ program. Create Quiz Comment S susobhanakhuli Follow 0 Improve S susobhanakhuli Follow 0 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