std::getline() in C++

Reading a file line-by-line is an important task in C++, which helps you to process the data stored in the external files efficiently. There are various methods to read a file line-by-line using the ifstream provided by the standard library. Here is the most common and simple method that you can use to read a file line-by-line, i.e., the std::getline() method in C++.

What is std::getline() in C++?

The std::getline() function in C++ is used to read an entire line of text in a file of an input stream, which stops at a specified delimiter ‘n’. It is most commonly used for reading strings with spaces.

Syntax:

std::getline(std::istream& stream, std::string& str, char delim = 'n');

Parameters:

  • stream: Input stream object from which the input is read.
  • str: Stores the extracted line from the input stream.
  • delim: Optional delimiter character that determines when to stop reading (default: n).

Read a File Line by Line Using std::getline() 

C++ provides the ifstream class, which helps you to read files efficiently. Ifstream stands for input file stream, which helps you to read the input data or texts of the files line-by-line. You can use the std::getline() function, as it is the most common method to read a file line-by-line while handling the whitespaces properly.

Example 1:

File name: sample.txt

Hello!
Welcome to Intellipaat!

Let’s read a file!

Cpp

Output:

Read a file using getline Example1

In the above code, firstly, the attached file is opened, then the getline() function reads one line at a time from the file, and the while loop checks that each line has been read till the end and prints the output, and after reading the data, the file is closed.

Get 100% Hike!

Master Most in Demand Skills Now!

Example 2:

File name: sample.txt

Hello!
Welcome to C++ Programming!

Let’s read a file using std::getline()!

Cpp

Output:

Read a file using getline Example2

In the above code, firstly, the attached file is opened, then the getline() function reads one line at a time from the file, and the while loop checks that each line has been read till the end and prints the output, and after reading the data, the file is closed.

Conclusion

Reading a file line-by-line in C++ is an essential part of processing data, and it can be done efficiently by using different methods. But the std::getline() method we have discussed here is very simple and easy to use, with handling the whitespaces correctly. This method simply allows you to process the text data efficiently. So, by understanding this method, you can read a file line-by-line efficiently.

Some Other Methods to Read a File Line by Line in C++

  • Read from a Binary File in C++
  • eof() in C++
  • getline() Function in C++
  • basic_istream::operator>> in C++
  • istream::readsome() in C++
  • std::ifstream::get() in C++

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner