lib/ispanstream.cpp

The following code example is taken from the book
C++23 - The Complete Guide by Nicolai M. Josuttis, Leanpub, 2026
The code is licensed under a Creative Commons Attribution 4.0 International License. Creative Commons License

// raw code

#include <print>
#include <spanstream>

int main()
{
  char input[] = "10 20 30 7.7";

  // initialize a span stream that reads from input:
  // - ensure the null terminator is not processed by the span stream
  std::ispanstream ispStrm{{input, sizeof(input)-1}};

  // as long as possible read ints and print them out:
  while (ispStrm) {
    int i;
    ispStrm >> i;
    if (ispStrm) {
      std::println("read {}", i);
    }
  }

  if (!ispStrm.eof()) {
    std::println("WARNING: not all input processed");
  }
}