How to Read Input Until EOF in C++? Last Updated : 31 Jan, 2024 Summarize Comments Improve Suggest changes Share 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. Comment More infoAdvertise with us Next Article How to Ask User Input Until Correct Input is Received? S susobhanakhuli 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 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 a Line of Input Text in C++? In C++, we often need to take input from the user by reading an input text line by line but the cin method only takes input till whilespace. In this article, we will look at how to read a full line of input in C++. For Example, Input: This is a text.Output: Entered Text: This is a text.Taking a Line 2 min read How to Ask User Input Until Correct Input is Received? User input is a common source of errors in C++ programs so itâs important to validate user input to make the program more reliable. In this article, we will discuss how to ask the user for input until valid input is received in our C++ program. For Example, Input: asdf Output: Incorrect Input. Pleas 2 min read How to Read a File Using ifstream in C++? 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 ifstre 2 min read How to Take Multiple Input from User in C++? In C++, we use cin when we want to take input from the user. We often also need to take more than one input at a time. In this article, we will learn how to take multiple inputs in C++. Take Multiple Inputs from a User in C++To take multiple inputs from users, we can repeatedly use the std::cin usin 2 min read 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 Like