filestream to read and write integer numbers
Hi,
I wrote a small program, which uses filestream to read and write integer numbers. The segment of code is as following:
int i, j;
while (!from.eof()) { // from is the input file
from >> i >> j;
std::cout << i <<j;
}
The input file includes int numbers 1 2 3 4. I expected that the output is 1 2 3 4. But it turns out to be 1 2 3 4 3 4.
I can't figure out why it produces wrong result. Can someone give some ideas?
Thanks!
Re: filestream to read and write integer numbers
Because eof() does not become true until you actually try reading past
the EOF (not after the last read).
Normally, you do not even check for eof() ...
Code:
while (from >> i >> j)
{
std::cout << i <<j;
}
Re: filestream to read and write integer numbers
Hi Philip,
It works. Thanks!
Jianhttp://www.codeguru.com/forum/newreply.php?do=newreply&noquote=1&p=2020317