Iterator

Discussion in 'C++' started by Fraser Ross, Feb 19, 2011.

  1. Fraser Ross

    Fraser Ross Guest

    typedef std::basic_ifstream<char> Stream;
    Stream fileStream;
    try {
    fileStream.open(Name().c_str(), std::ios_base::binary);
    if (fileStream.is_open()) {
    typedef std::istream_iterator<ColourType, Stream::char_type> Iter;
    bool const test2(Iter() == Iter(fileStream));

    Why is test2 set to true? The file being read is ok. ColorType is a 32
    bit integer.

    Fraser.
    Fraser Ross, Feb 19, 2011
    #1
    1. Advertising

  2. Fraser Ross

    Bo Persson Guest

    Fraser Ross wrote:
    > typedef std::basic_ifstream<char> Stream;
    > Stream fileStream;
    > try {
    > fileStream.open(Name().c_str(), std::ios_base::binary);
    > if (fileStream.is_open()) {
    > typedef std::istream_iterator<ColourType, Stream::char_type> Iter;
    > bool const test2(Iter() == Iter(fileStream));
    >
    > Why is test2 set to true? The file being read is ok. ColorType is
    > a 32 bit integer.
    >
    > Fraser.


    I believe the iterator constructor is allowed to read ahead one value
    (and cache it). If that fails for some reason, the iterator will be
    equal to the end-of-file iterator.


    Bo Persson
    Bo Persson, Feb 20, 2011
    #2
    1. Advertising

  3. Fraser Ross

    Fraser Ross Guest

    "Bo Persson"
    > I believe the iterator constructor is allowed to read ahead one value
    > (and cache it). If that fails for some reason, the iterator will be
    > equal to the end-of-file iterator.



    I think I've made a basic mistake expecting to be able to read anything
    other than characters from a stream opened in binary mode. It would be
    useful if it was indicated somehow even at runtime after a debug build.

    Fraser.
    Fraser Ross, Feb 20, 2011
    #3
  4. Fraser Ross

    James Kanze Guest

    On Feb 19, 8:35 pm, "Fraser Ross" <> wrote:
    > typedef std::basic_ifstream<char> Stream;
    > Stream fileStream;
    > try {
    > fileStream.open(Name().c_str(), std::ios_base::binary);
    > if (fileStream.is_open()) {
    > typedef std::istream_iterator<ColourType, Stream::char_type> Iter;
    > bool const test2(Iter() == Iter(fileStream));


    > Why is test2 set to true? The file being read is ok. ColorType is a 32
    > bit integer.


    The istream_iterators read ahead. (They have to, in order to
    know when end of file is reached *before* they are
    dereferenced.) You haven't told us what the file contains, but
    the istream_iterators use << to read: a formatted input
    function. If the data in the input stream is not formatted
    correctly---for an integer, this means that the next non white
    space is a digit or a sign followed by a digit, the input fails,
    which terminates the iteration.

    After the end of the iteration, it might be useful to check why
    the iteration terminated: if !fileStream.eof(), it was due to
    a format error (or some hardware failure, if fileStream.bad()).

    --
    James Kanze
    James Kanze, Feb 21, 2011
    #4
  5. Fraser Ross

    James Kanze Guest

    On Feb 20, 2:13 pm, "Fraser Ross" <> wrote:
    > "Bo Persson"


    > > I believe the iterator constructor is allowed to read ahead one value
    > > (and cache it). If that fails for some reason, the iterator will be
    > > equal to the end-of-file iterator.


    > I think I've made a basic mistake expecting to be able to read anything
    > other than characters from a stream opened in binary mode. It would be
    > useful if it was indicated somehow even at runtime after a debug build.


    If what was indicated? It's up to you to check the istream's
    state, if it is relevant to your program. (It almost always is
    when reading from a file. When reading from an istringstream
    initialized from a text box in a window, or something like that,
    it might not be.)

    --
    James Kanze
    James Kanze, Feb 21, 2011
    #5
    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. Hendrik Maryns
    Replies:
    18
    Views:
    1,395
  2. greg
    Replies:
    6
    Views:
    441
    Dietmar Kuehl
    Jul 17, 2003
  3. Replies:
    6
    Views:
    620
    Jim Langston
    Oct 30, 2005
  4. Steven D'Aprano

    What makes an iterator an iterator?

    Steven D'Aprano, Apr 18, 2007, in forum: Python
    Replies:
    28
    Views:
    1,122
    Steven D'Aprano
    Apr 20, 2007
  5. David Bilsby
    Replies:
    5
    Views:
    2,019
    David Bilsby
    Oct 9, 2007
Loading...

Share This Page