"EOF"??

S

sofin

File:
1 2 3 4 5 6
I want to read the data.


ifstream FP;
FP.open("file name");

int temp;

do {

FP >> temp;
cout << temp;
}while(FP->eof());


Result" 1234566

Why the last digit 6 repeats twice?
 
J

James Kanze

Surely it's
}while (!FP->eof());
I believe it's covered in the FAQ. The EOF condition is not
set after the last number is read *successfully*.

Actually, it's unspecified whether it is set or not. If the
last character in his file is '6' (and not e.g. '\n'), then it
probably would be set.

In practice, you never check eof until you've detected failure.
If an input fails, and eof isn't set, then you know you have an
error. If it is set, you've probably reached the end of file
(but there are some special cases where it will be set even in
case of an error).
It's set when reading *fails* because the file is at the end.
Since you output 'temp' regardless of the success of the
extraction on the line before, you get it printed twice, and
the 'do-while' loop exits only when 'temp' is not read. Fix
it by doing:
do {
if (FP >> temp)
cout << temp;
} while (!FP->eof());

Or far more idiomatically:

while ( FP >> temp ) {
cout << temp ;
}
 
D

Daniel Pitts

sofin said:
File:
1 2 3 4 5 6
I want to read the data.


ifstream FP;
FP.open("file name");

int temp;

do {

FP >> temp;
cout << temp;
}while(FP->eof());


Result" 1234566

Why the last digit 6 repeats twice?

while (!(FP >> temp).eof()) {
std::cout << temp << " ";
}
 
J

James Kanze

while (!(FP >> temp).eof()) {
std::cout << temp << " ";
}

No.

Remove the final white space (the new line), and there's a
distinct chance that the code won't display the final '6'.

See my other post for the correct way to do it. But in any
case, don't ever use ios::eof() before an input has failed.
 
D

Daniel Pitts

James said:
No.

Remove the final white space (the new line), and there's a
distinct chance that the code won't display the final '6'.

See my other post for the correct way to do it. But in any
case, don't ever use ios::eof() before an input has failed.

Can you please explain that to me? I thought that eof flag was only set
once an attempted read resulted in passing (not reaching) the end of file.
 
D

Daniel Pitts

Victor said:
I believe that if your file *ends* on the '6' (and not on a newline,
say), then the EOF condition would be set *while* reading the last
number (that just happens to have a single digit). The operation will
succeed in converting the input to the int ('temp'), so a simple check
of the return of (FP >> temp) would yield 'true', yet calling 'eof()'
would also yield 'true'. That's what might happen.

V

It is all well and good that you believe that, can you point to a
resource that specifies it may be so?

I'm not trying to troll here. I really didn't know this was true, and
I'd like to know where I can read the details of it.

Thanks,
Daniel.
 
J

James Kanze

It is all well and good that you believe that, can you point
to a resource that specifies it may be so?
I'm not trying to troll here. I really didn't know this was
true, and I'd like to know where I can read the details of it.

I'd be more interesting in knowing what reference said anything
else. This has been the case since classical IO streams, and is
essential to know when writing your own << operators. And the
standard is quite clear about it. (Historically, many early
implementations of filebuf wouldn't necessarily give a second
EOF after sgetc() returned EOF once, at least if they were
inputting from the keyboard. So it was important that the
istream memorize the EOF, and not make any further calls to
sgetc().)

More generally, I'm curious about where so many people are
learning things like:

while ( ! std::cin.eof() ) {
// read a value and us it without further tests...
}

I can't find anything like that in any of the accepted
references (Josuttis, etc.). Josuttis says clearly that eofbit
is set if end-of-file was encountered. Not that an operation
failed. Dinkumware says the same thing. (And of course, it's
what the standard says as well.)
 

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,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top