Cant create vector from istream_iterator

Discussion in 'C++' started by DeveloperDave, Feb 22, 2010.

  1. Hi,
    I am having trouble constructing a vector using some istream_iterators
    and a file.....

    I have written some code that opens a file and creates a few
    istream_iterators.
    i.e.

    std::fstream file(filename.c_str(), (ios_base::in |
    ios_base::binary));
    istream_iterator<unsigned char>pos = istream_iterator<unsigned
    char>(file);
    istream_iterator<unsigned char>start = pos;
    istream_iterator<unsigned char>end;

    I increment the 'pos' iterator, to iterate through the file. At a
    certian point I update 'end' to be equal to 'pos'.
    I can print out the values that 'start' and 'end' both reference and
    see they are the bytes from the file that I would expect, but when I
    try to create a vector, it is always empty e.g.

    vector<unsigned char>myVector(start, end);

    myVector.size() always returns 0.

    Am I using the istream_iterator in the wrong way?

    Thanks
     
    DeveloperDave, Feb 22, 2010
    #1
    1. Advertising

  2. DeveloperDave

    Bo Persson Guest

    DeveloperDave wrote:
    > Hi,
    > I am having trouble constructing a vector using some
    > istream_iterators and a file.....
    >
    > I have written some code that opens a file and creates a few
    > istream_iterators.
    > i.e.
    >
    > std::fstream file(filename.c_str(), (ios_base::in |
    > ios_base::binary));
    > istream_iterator<unsigned char>pos = istream_iterator<unsigned
    > char>(file);
    > istream_iterator<unsigned char>start = pos;
    > istream_iterator<unsigned char>end;
    >
    > I increment the 'pos' iterator, to iterate through the file. At a
    > certian point I update 'end' to be equal to 'pos'.
    > I can print out the values that 'start' and 'end' both reference and
    > see they are the bytes from the file that I would expect, but when I
    > try to create a vector, it is always empty e.g.
    >
    > vector<unsigned char>myVector(start, end);
    >
    > myVector.size() always returns 0.
    >
    > Am I using the istream_iterator in the wrong way?
    >


    Yes. :)

    The istream_iterator is an input iterator, which only has to support a
    single pass through the sequence.

    To do what you attempt requires a forward iterator, which would allow
    you to do mutliple paees over the sequence.

    http://www.sgi.com/tech/stl/Iterators.html


    In your particular case, perhaps you can push_back each character
    during your first (and only!) pass?



    Bo Persson
     
    Bo Persson, Feb 22, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    24,132
  2. Replies:
    8
    Views:
    2,001
    Csaba
    Feb 18, 2006
  3. Replies:
    3
    Views:
    486
    Johan Bengtsson
    Jun 30, 2006
  4. Nagaraj
    Replies:
    1
    Views:
    920
    Lionel B
    Mar 1, 2007
  5. Javier
    Replies:
    2
    Views:
    624
    James Kanze
    Sep 4, 2007
Loading...

Share This Page