Save istream position for later use

818 views Asked by At

I wrote the code below which should save istream iterators. What I want is to keep the iterator for later copy to a string at the given position. This works fine until I reset the stream to the beginning. The output is "teet" bis I wanted "tees". Is there any way to make an iterator independent from the stream position?

#include <iostream>
#include <sstream>
#include <iterator>

int main(){
    std::stringstream test;
    test << "test 123456\n";

    std::istream_iterator<char> it(test);
    std::cout << *it;
    it++;
    std::cout << *it;

    test.clear();
    test.seekg(0, std::ios::beg);

    std::cout << *it;
    it++;
    std::cout << *it;
}
0

There are 0 answers