C++ Read file data line by line


In all types of programming languages, file management is a fundamental concept. This file-handling concept is also a core point in C++. In the digital age, we only deal with data that is stored in files on storage disks. This language gives various tools to efficiently handle file operations such as creating, writing, reading, opening and adding data to files in the system. In this article, our main focus is to learn how to easily read a file line by line in C++ language and provide some valid examples and output proofs for more enhancements.

File input in C++

It is fundamental to understand file input in C++ before reading files. In C++, we “” which embeds the functionality of reading and writing data to files. To read the file, we use the “ifstream” class. First, you need to One must open any C++ editor and then open the text file that is to be read. The “IfStream” class simplifies the process by enabling the pairing of a file with a stream object.

Different ways to read files line by line in C++

In this section, we will look at the methods that are used to read data in files in C++:

These are two methods through which files are easily read line by line in C++. Let us explain each method separately for better understanding. We will discuss about get() function after getline() function. The complete code of both these functions is also attached. It is recommended that you execute this code on the system, view the output, and understand how each line of code works.

Method 1: Read file using Getline() in C++

In C++ the getline() method is used to output data from a file line by line. This is a predefined method which means it is already “” is defined in the library. The getLine() function converts user input to a character delimiter string and stores it in a variable.

Syntax of getline():

Once the getLine() function arrives at the char delimiters, it reads the characters from the file stream and then stores them in a variable string.

The getLine() method syntax is given as follows:

get line ,Stream, is, string, STR,,

Here, the getline() function has three parameters: “istream& is”, “string&”, “str”.

iStream is: This is the input stream for standard input, such as 'sin', to read the line.

string&str: The string into which the input streamline is read will be stored. The text that is input is stored in this variable string.

Let us take an example to understand the concept of this function better. First, declare the library in the file. After that, take the variable whose type is “String” and take the entire program in the main function. Pass the streamline type such as “sin” and the variable to the getLine() function. “syn” is the input stream object that takes input from the user at runtime. We display the results on the output screen using the “Count Stream” object. Remember that all the code written inside the main function, that is int main(), gets executed and gives us our required output.


#Involved <स्ट्रिंग>
using namespace std;
int main,, ,
string read_data;
get line ,scene,read_data,,
return 0,

After that, click on “Execute” option from the menu ribbon. Select the “Compile and Run” option to quickly execute the program. The output screen will appear. Then, type the text you want to read. Press the “Enter” button which easily reads the entire text line by line. The output of this example is as follows:

Method 2: Read file using Get() function in C++

Using this get() function, we read one character at a time. It does not ignore spaces and treats it as a collection of characters. First, add some data to the text file.

Syntax of get() function:

The get() method syntax is given as follows:

Here, file is “&istream” which takes the file from the desired location, and filename is the name of the text file whose data needs to be read.

Let us take example in which we discuss get() function. First, we create a text file that captures readable data. Here, we take a text file (named “xyz.txt”) mentioned as follows:

After that open C++ code editor. Type the code shown in the attached screenshot.

int main,, ,
std::ifstream file,“xyz.txt”,,
If ,,file.open,,, ,
std::cout , “The file is not open\n,,
return 1,
char read_file;
Whereas ,file.get,read_file,, ,
std::cout , read_file; , print character to console
file.close,,, , turn it off file
return 0,

First, add the corresponding library at the beginning of the program. In the main function, keep the location of the file using the “ifstream” function using the “file” variable. After that, if the file is not open or there is no exact problem, an error “The file is not open” is displayed on the console window.

Have another variable called “read_file” with “character” data type. Pass this character variable to get() function. This function reads characters one by one and line-by-line until it runs out of file data. Display these data on the console window using the object “cout” of the output stream which is used to display the output. Finally, use the close() function to close the file that we opened for reading. The output of this program is attached in the following screenshot:


In short, it is said that we easily read file data lines in C++ programming language by following their syntax and structure. libraries or header files always required to implement the desired output Include; Is used for file management. We choose the appropriate method to handle the files to completely read the data line by line. Always use the correct function name to open and close a text file for reading purposes. We discussed everything with proper examples and screenshots to help you.

Add comment

By Ranjan