Using streams on fixed-size external buffers
The <strstream>
header has been part of the standard I/O library from its beginning. It contains classes that provide stream operations on sequences of characters stored in an array. However, this header was deprecated a long time ago, in C++98, although it’s still available because a replacement wasn’t available. The C++20 standard has introduced the std::span
class, which is a non-owning view of a sequence of objects. In C++23, a new header, <spanstream>
, has been added as a replacement for <strstream>
. This contains classes that provide stream operations on externally provided memory buffers. In this recipe, we’ll learn how to parse or write text using the I/O span streams.
How to do it…
Use the new C++23 span streams as follows:
- To parse text from an external array, use
std::ispanstream
:char text[] = "1 1 2 3 5 8"; std::ispanstream is{ std::span<char...