std::istreambuf_iterator<char> first( _file ); std::istreambuf_iterator<char> last( _file ); std::advance( last, _size );
istreambuf_iterators are input iterators. As soon as you advance last, the iterator changes. You see them as Forward Iterators, which have a property that you can copy an iterator, promote it, and then get an identical sequence by promoting the copy.
In general:
template<class InIter, class Size, class OutIter> void copy_n(InIter begin, InIter end, Size n, OutIter dest) { for (; begin != end && n > 0; ++begin, --n) { *dest++ = *begin; } }
However, in this case, you are better off resizing the string using istream :: read directly in & result [0] and finally checking that you are reading the correct number of characters.
Roger Pate
source share