Skip to content
C++: Preserve a trailing empty field with getline

C++: Preserve a trailing empty field with getline

November 11, 2026

For a simple delimited record, red,,blue, contains four fields. A loop around std::getline finds the empty middle field but needs an explicit rule for the final empty field. Decide separately what an entirely empty record means.

Prepare a scratch directory

Tested on Linux with GCC 14.2.0 in C++20 mode. You need Bash, a C++20 compiler and standard library, a text editor, and the mktemp, rm, and rmdir utilities. Keep the same terminal open. Stop if setup fails, before saving the source:

start_directory="$PWD"
work_directory="$(mktemp -d)" || exit 1
cd -- "$work_directory" || exit 1

Run the example

Save as cpp-getline-trailing-empty-field.cpp:

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

std::vector<std::string> split_record(const std::string& text) {
    std::vector<std::string> fields;
    std::istringstream input(text);
    std::string field;
    while (std::getline(input, field, ',')) fields.push_back(field);
    if (text.empty() || text.back() == ',') fields.emplace_back();
    return fields;
}

int main() {
    assert((split_record("red,,blue,") ==
            std::vector<std::string>{"red", "", "blue", ""}));
    assert((split_record("") == std::vector<std::string>{""}));
    assert((split_record(",") == std::vector<std::string>{"", ""}));
    for (const std::string text : {"red,,blue,", "", ",", "red"}) {
        const auto fields = split_record(text);
        std::cout << fields.size() << " fields:";
        for (const auto& field : fields) std::cout << " [" << field << ']';
        std::cout << '\n';
    }
}

Compile and run:

g++ -std=c++20 -Wall -Wextra -Werror -pedantic cpp-getline-trailing-empty-field.cpp -o cpp-getline-trailing-empty-field &&
./cpp-getline-trailing-empty-field

Observed output:

4 fields: [red] [] [blue] []
1 fields: []
2 fields: [] []
1 fields: [red]

How it works

getline consumes the delimiter without adding it to the returned string. Consecutive commas therefore produce an empty field successfully. After the last comma, the next call reaches EOF without extracting anything and fails; the loop body does not run for that final field.

The post-loop condition restores that field. The empty-record policy in this function is one empty field, which makes a lone comma produce two empty fields. These choices are part of the format contract and are asserted in the program.

Limits and choices

This format has no quoting or escaping. A comma inside quotation marks still separates fields, and newline characters inside the supplied string are ordinary field content. Use a CSV parser for CSV input. The function copies fields into owned strings and allocates in proportion to the supplied record; limit record size before calling it for untrusted data.

The C++ working draft specifies the library operation used here. The example uses the C++20 interface tested above; newer draft additions are outside its scope.

Clean up

rm -- cpp-getline-trailing-empty-field.cpp cpp-getline-trailing-empty-field
cd -- "$start_directory" || exit 1
rmdir -- "$work_directory"
unset start_directory work_directory

If the directory is not empty, inspect the remaining files before removing anything else.

Acknowledgements

This post was created with assistance from AI.

Last updated on