Search code examples
c++iteratoristream-iterator

File istream iterator understanding


I am probably lacking some understanding of the istream_iterator or iterators in general, but should not these two code snippets produce the same results?

typedef vector<int>::iterator Itv;
vector<int> vec = {1,2,3,4,5,6,7,8,9};

Itv ev = vec.begin();
++ev;
++ev;
++ev;
for (Itv it = vec.begin(); it != ev; ++it){
    cout << *it;
}

Outputs: 123

//data.txt contains: 1,2,3,4,5,6,7,8,9
typedef istream_iterator<char> Itf;
fstream file("data.txt");
Itf ef(file);
++ef;
++ef;
++ef;    
for (Itf it(file); it != ef; ++it){
    cout << *it;
}

Outputs nothing.


Solution

  • Input stream iterators are single-pass iterators, meaning once they have been incremented its previous value cannot be obtained. When incrementing std::istream_iterator<T> it extracts a value into a T() object and returns the result. Consequently, the value is discarded and cannot be re-obtained.

    What you have to do is store each read into a vector and print it out afterwards. You can also use std::ostream_iterator:

    std::ostream_iterator<char> out(std::cout);
    Itf ef(file);
    
    out = ++ef;
    out = ++ef;
    out = ++ef;
    

    This is also equivalent:

    std::copy(ef, Itf(), out);