-
June 16th, 2011, 10:20 AM
#1
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!
-
June 16th, 2011, 10:24 AM
#2
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;
}
-
June 16th, 2011, 10:31 AM
#3
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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|