while(!FP.eof()) is reading the last data point of the file twice

R

ramana

I'm wondering if someone could point me to the flaw in the following
code that uses the while(!FP.eof()) condition to read the input data.
This condition is reading the last data point of the file twice.

#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char **argv)
{
double x;
ifstream FP("test.d");

//while(!FP.eof()){FP >> x; cout << x << endl;} // This reads the
last data point of test.d twice
while(FP >> x){cout<< x << endl;} // This doesn't.

return 0;
}
/* Using either gcc 3.4.6 or gcc 4.1.3
File "test.d" has the following 2 data points:
1.1
2.2
*/

Thanks...ramana
 
K

Keith Thompson

ramana said:
I'm wondering if someone could point me to the flaw in the following
code that uses the while(!FP.eof()) condition to read the input data.
This condition is reading the last data point of the file twice.

#include <iostream>
#include <fstream>
using namespace std;
[snip]

That's C++, not C. Try asking in comp.lang.c++ (but check the C++ FAQ
first; Google will tell you where it is).
 
S

santosh

ramana wrote:

<snip>

Please post C++ to comp.lang.c++ or alt.comp.lang.learn.c-c++.
 
M

Mark McIntyre

I'm wondering if someone could point me to the flaw in the following
code that uses the while(!FP.eof()) condition to read the input data.
This condition is reading the last data point of the file twice.

This is C++, but the answer is probably the same as that for the C FAQ
relating to feof().
 
M

Martin Ambuhl

ramana said:
I'm wondering if someone could point me to the flaw in the following
code that uses the while(!FP.eof()) condition to read the input data.
This condition is reading the last data point of the file twice.

#include <iostream>

What's wrong is that you don't know what language you are using.
<is for discussions of C, not for C++.
 
R

ramana

What's wrong is that you don't know what language you are using.
<news:comp.lang.c> is for discussions of C, not for C++.


I do.

First, I want to apologize for the wrong post. I never meant to post
this to lang.c, it was an unintended mistake. Thanks nevertheless for
those who didn't ridicule but gave useful tips...ramana
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top