Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Iterator

Reply
Thread Tools

Iterator

 
 
Fraser Ross
Guest
Posts: n/a
 
      02-19-2011
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.


 
Reply With Quote
 
 
 
 
Bo Persson
Guest
Posts: n/a
 
      02-20-2011
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


 
Reply With Quote
 
 
 
 
Fraser Ross
Guest
Posts: n/a
 
      02-20-2011
"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.


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      02-21-2011
On Feb 19, 8:35 pm, "Fraser Ross" <z...@z.com> 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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      02-21-2011
On Feb 20, 2:13 pm, "Fraser Ross" <z...@z.com> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List iterator assignment fails, assert iterator not dereferencable David Bilsby C++ 5 10-09-2007 02:05 PM
What makes an iterator an iterator? Steven D'Aprano Python 28 04-20-2007 03:34 AM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57