Iterator

F

Fraser Ross

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.
 
B

Bo Persson

Fraser said:
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
 
F

Fraser Ross

"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.
 
J

James Kanze

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()).
 
J

James Kanze

"Bo Persson"
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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top