Input Streams

Using Custom Delimiters with std::getline()

How can I use std::getline() with a custom delimiter?

Abstract art representing computer programming

Using std::getline() with a custom delimiter is straightforward and allows you to read input up to the specified character. This is useful when dealing with structured data formats where different delimiters are used.

Basic Usage

By default, std::getline() reads input until a newline character is encountered. You can provide a custom delimiter as the third argument to std::getline():

#include <iostream>
#include <sstream>
#include <string>

int main() {
  std::string input = "Hello,World,from,C++";
  std::istringstream stream(input);
  std::string word;

  while (std::getline(stream, word, ',')) {  
    std::cout << "Extracted: " << word << '\n';
  }
}
Extracted: Hello
Extracted: World
Extracted: from
Extracted: C++

In this example, std::getline() uses a comma as the delimiter, extracting substrings separated by commas.

Handling Complex Delimiters

For more complex delimiters or multi-character separators, you might need to manually parse the input. However, for single-character delimiters, std::getline() is very effective.

Example with Custom Delimiter

Consider a scenario where data entries are separated by a semicolon:

#include <iostream>
#include <sstream>
#include <string>
#include <vector>

int main() {
  std::string input = "Legolas;Gimli;Aragorn";
  std::istringstream stream(input);
  std::string name;
  std::vector<std::string> names;

  while (std::getline(stream, name, ';')) {  
    names.push_back(name);
  }

  for (const auto& n : names) {
    std::cout << "Name: " << n << '\n';
  }
}
Name: Legolas
Name: Gimli
Name: Aragorn

In this example, std::getline() reads the names separated by semicolons and stores them in a vector.

Advantages of Using Custom Delimiters

  • Flexibility: Easily adapt to various data formats.
  • Simplicity: Simplify code for parsing structured data.
  • Performance: Efficiently read input without manual parsing loops.

Error Handling

Always check the stream state after reading to handle any potential errors or unexpected input. Using a loop with std::getline() ensures robust error handling.

#include <iostream>
#include <sstream>
#include <string>

int main() {
  std::string input = "Legolas;Gimli;Aragorn";
  std::istringstream stream(input);
  std::string name;

  while (std::getline(stream, name, ';')) {
    if (name.empty()) {  
      std::cerr << "Empty entry found\n";
    } else {
      std::cout << "Name: " << name << '\n';
    }
  }
}
Name: Legolas
Name: Gimli
Name: Aragorn

Using custom delimiters with std::getline() provides a powerful tool for reading and processing structured input, making it versatile for various applications in C++.

This Question is from the Lesson:

Input Streams

A detailed introduction to C++ Input Streams using std::cin and istringstream. Starting from the basics and progressing up to advanced use cases including creating collections of custom objects from our streams.

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Input Streams

A detailed introduction to C++ Input Streams using std::cin and istringstream. Starting from the basics and progressing up to advanced use cases including creating collections of custom objects from our streams.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved