Skip to content
C++: Reject trailing input in a stream record

C++: Reject trailing input in a stream record

November 11, 2026

A two-column record can contain two valid integers followed by accidental garbage. Checking only the first two extractions accepts that record. A final extraction can distinguish harmless trailing whitespace from an extra character.

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-stream-record-trailing-input.cpp:

#include <cassert>
#include <iostream>
#include <locale>
#include <optional>
#include <sstream>
#include <string>
#include <utility>

std::optional<std::pair<int, int>> parse_pair(const std::string& text) {
    std::istringstream input(text);
    input.imbue(std::locale::classic());
    int first = 0;
    int second = 0;
    if (!(input >> first >> second)) return std::nullopt;
    char extra = '\0';
    if (input >> extra) return std::nullopt;
    if (!input.eof()) return std::nullopt;
    return std::pair{first, second};
}

int main() {
    assert((parse_pair("10 20") == std::pair{10, 20}));
    assert(!parse_pair("10"));
    assert(!parse_pair("999999999999999999999 20"));
    for (const std::string text : {"10 20", "10 20   ", "10 20x", "10 20 30", "10"}) {
        const auto value = parse_pair(text);
        std::cout << '[' << text << "]: ";
        if (value) std::cout << value->first << ',' << value->second << '\n';
        else std::cout << "rejected\n";
    }
}

Compile and run:

g++ -std=c++20 -Wall -Wextra -Werror -pedantic cpp-stream-record-trailing-input.cpp -o cpp-stream-record-trailing-input &&
./cpp-stream-record-trailing-input

Observed output:

[10 20]: 10,20
[10 20   ]: 10,20
[10 20x]: rejected
[10 20 30]: rejected
[10]: rejected

How it works

The character extraction uses the stream’s default whitespace skipping. If it succeeds, an extra non-whitespace character exists and the record is rejected. If it fails, the EOF check confirms that the stream ran out of input. The first two extractions are checked before any values are returned.

Using the classic locale makes the parsing independent of the process’s global locale. The accepted grammar is still the stream’s integer grammar: signs and leading whitespace are allowed. This is useful for small whitespace-separated records, but it is not a CSV parser.

Limits and choices

The input string is already in memory. Put a size limit on record acquisition when reading external files. A rejected record leaves no partial pair for the caller to use. If the format requires a particular separator or forbids signs, check that grammar explicitly instead of relying on formatted extraction. The failed final probe changes the local stream state, which is harmless here because the function discards that stream.

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-stream-record-trailing-input.cpp cpp-stream-record-trailing-input
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