I have an istream
and need to copy the content between two delimiters to a std::string
.
I can find the delimiters' streampos
, but when trying to use istream_iterator<char>
to iterate over the section of the stream, it does not work. Here's what I tried:
#include <iostream>
#include <sstream>
#include <iterator>
std::string copyToString( std::istream& is )
{
is >> std::ws;
auto someLength {10};
std::istream_iterator<char> beg {is};
is.seekg( someLength, std::ios::cur );
//std::istream_iterator<char> end { is };
std::istream_iterator<char> end { std::next(beg, someLength) };
return std::string{ beg, end };
}
int main()
{
std::stringstream ss;
ss.str( " { my string content }" );
std::cout << "\"" << copyToString( ss ) << "\"\n";
return 0;
}
I expected the output to be 10 chars long, but it's just "{"
. If I uncomment the line std::istream_iterator<char> end { is };
and comment out the next one, the output is just ""
.
What am I missing? Can I not use iterators like that?
I am aware that I could create a char[]
as a buffer, copy it to that, etc., but that seems much less straightforward.