Support Online
Skip to main content

C++ std::getline() Usage

What Will You Learn in This Guide?

In this guide, you will learn how the std::getline() function works in C++.
You'll read data line by line from the keyboard or file, correctly capture input containing spaces, and discover how to avoid common mistakes like the newline error.

Technical Summary

  • Main topic: std::getline() function in C++ Standard Library
  • Purpose: To read data from input streams (cin, ifstream, stringstream) by line or special brackets
  • Advantage: Getting full line data by including space characters
  • Difficulty: Misreading the remaining character \n after cin >>
  • Solution: Using “dummy getline()” or cin.ignore()

What is std::getline()?

The std::getline() function reads characters from an input stream and assigns them to a std::string variable.
This also reads spaces, unlike the cin >> operator.

istream& getline(istream& input_stream, string& output, char delim);

Parameters:

input_stream: Source of the input (std::cin, file stream, string stream)

output: std::string variable where the read characters will be written

delim (optional): Delimiter to stop reading (default \n)

  • The function belongs to the header file <string> because it works directly with the std::string type.

1. Reading Lines from the Keyboard


#include <iostream>
#include <string>
using namespace std;

int main() {
string isim;
cout << "Adınızı girin: ";
getline(cin, isim);
cout << "Merhaba, " << isim << "!\n";
}
  • Explanation: Since getline() also reads spaces, data such as "Ahmet Yılmaz" is retrieved without any problems.

2. Reading Line by Line from a File

Let's read the genixnode_veri.txt file on a cloud server line by line 👇


#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

int main() {
ifstream dosya("genixnode_veri.txt");
vector<string> satirlar;
string temp;

while (getline(dosya, temp)) {
satirlar.push_back(temp);
}

for (const auto& s : satirlar)
cout << s << endl;
}

Sample File (genixnode_veri.txt):

Merhaba GenixNode
İkinci satır
Son satır

Output:


Merhaba GenixNode
İkinci satır
Son satır

3. Using Special Delimiters

By default, getline() reads up to the end of line \n character. But we can change this delimiter if we want.


while (getline(dosya, temp, ' ')) {
satirlar.push_back(temp);
}
  • Description:

In the above example, since the separator is ' ' (space), each word is read separately.


4. Empty Line Error After std::cin

Calling getline() immediately after using std::cin >> usually returns null. The reason is the \n character remaining in the stream after the cin read operation.

Faulty Example:


int id;
string isim;
cin >> id;
getline(cin, isim); // isim boş kalır!

Solution 1 – Dummy getline():


int id;
string isim;
cin >> id;
string dummy;
getline(cin, dummy); // Temizleme
getline(cin, isim);

Solution 2 – Professional method (cin.ignore()):


#include <limits>
cin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(cin, isim);
  • Description:

cin.ignore() ignores the \n character in the stream and makes the actual line input ready to read.


Performance Features

FeatureDescription
ApproachReading by line or special bracket
Time ComplexityO(N)
ScopeCompatible with cin, ifstream, stringstream
AdvantageEase of reading lines including spaces
Potential ProblemThe remaining character newline (\n) after cin is

Frequently Asked Questions (FAQ)

  1. Why is getline() defined in the <string> header?

Because it works directly with std::string objects, so it's located in <string> and not <iostream>.

  1. Why does getline() preserve spaces?

Because it considers all characters up to the delimiter as "data".

  1. What is the difference between cin.ignore() and dummy getline()?

They both do the same job; ignore() is a more controlled and professional method.

  1. Does getline() only work with file streams?

No, it can be used in all streams (cin, ifstream, stringstream) derived from std::istream.

  1. Is getline() reliable in terms of performance?

Yes, it reads even large files efficiently with O(N) complexity.


Result

In this guide, you learned how to read data using the std::getline() function in C++, both via the keyboard and the file. We also saw how to fix the post-cin newline error.

Start your C++ development environment on the GenixNode platform and try these examples now!