473,241 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

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


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

Dec 25 '07 #1
7 3976
LR
ramana wrote:
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.
I don't think so.
>
#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
Are you sure about that?
while(FP >x){cout<< x << endl;} // This doesn't.
I suspect, for many, but not all, apps this would be preferred over the
commented code above.

>
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
May I suggest that you try this code,

#include <iostream>
#include <fstream>

int main() {
std::ifstream in("junk.txt");
while(!in.eof()) {
double d = 999.;
in >d;
std::cout << d << std::endl;
}
}

And this,
#include <iostream>
#include <fstream>

int main() {
std::ifstream in("junk.txt");
while(!in.eof()) {
double d = 999.;
if(in >d) {
std::cout << d << std::endl;
}
}
}
Dec 26 '07 #2
ramana <ra**********@gmail.comwrote:
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
The reason the last data point is "read" twice is this... When the last
data point is read the first time, eof() still false because you haven't
reached the end of the file yet, then it attempts to read another data
point, but none exists, so eof is set to true and x is left unchanged,
so the last data point outputs a second time.
while(FP >x){cout<< x << endl;} // This doesn't.
This one works as follows... The last data point is read, then FP is
true so the data point is printed out, then when it goes to read more,
there isn't any os FP returns false and the loop isn't executed anymore.
Dec 26 '07 #3
//while(!FP.eof()){FP >x; cout << x << endl;} // This reads the
last data point of test.d twice

The reason the last data point is "read" twice is this... When the last
data point is read the first time, eof() still false because you haven't
reached the end of the file yet, then it attempts to read another data
point, but none exists, so eof is set to true and x is left unchanged,
so the last data point outputs a second time.
while(FP >x){cout<< x << endl;} // This doesn't.

This one works as follows... The last data point is read, then FP is
true so the data point is printed out, then when it goes to read more,
there isn't any os FP returns false and the loop isn't executed anymore.
Thank you Daniel... It's very enlightening...ramana
Dec 26 '07 #4

Thank you LR...
May I suggest that you try this code,

#include <iostream>
#include <fstream>

int main() {
std::ifstream in("junk.txt");
while(!in.eof()) {
double d = 999.;
in >d;
std::cout << d << std::endl;
}

}

This code still printed 999 after the contents of junk.txt. It's quite
revealing though...

And this,
#include <iostream>
#include <fstream>

int main() {
std::ifstream in("junk.txt");
while(!in.eof()) {
double d = 999.;
if(in >d) {
std::cout << d << std::endl;
}
}

}
This code worked perfect.
And as Daniel's reply would mean, failing if-condition prevented its
stmnts from executing... Thanks. ramana

Dec 26 '07 #5
ramana wrote:
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;
}

This is a FAQ, 15.5
http://parashift.com/c++-faq-lite/in....html#faq-15.5

Essentially, C++ doesn't return true until *after* you've read EOF. So
you hit EOF. The subsequent read doesn't change your data, so you get
it twice. Then testing EOF indicates eof.

The FAQ is more eloquent than I am.
Dec 26 '07 #6
On Dec 26, 8:22*am, red floyd <no.s...@here.dudewrote:
ramana wrote:
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;
}

This is a FAQ, 15.5http://parashift.com/c++-faq-lite/input-output.html#faq-15.5

Essentially, C++ doesn't return true until *after* you've read EOF. *So
you hit EOF. *The subsequent read doesn't change your data, so you get
it twice. *Then testing EOF indicates eof.

The FAQ is more eloquent than I am.- Hide quoted text -

- Show quoted text -
I think main reason for the above mentioned behaviour is you might be
having SPACE or END OF LINE character at the end of the file.
Dec 26 '07 #7
Sachin wrote:
On Dec 26, 8:22 am, red floyd <no.s...@here.dudewrote:
>ramana wrote:
>>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;
}
This is a FAQ, 15.5http://parashift.com/c++-faq-lite/input-output.html#faq-15.5

Essentially, C++ doesn't return true until *after* you've read EOF. So
you hit EOF. The subsequent read doesn't change your data, so you get
it twice. Then testing EOF indicates eof.

The FAQ is more eloquent than I am.- Hide quoted text -

- Show quoted text -

I think main reason for the above mentioned behaviour is you might be
having SPACE or END OF LINE character at the end of the file.
No, it's because the stream doesn't know it's at EOF until it's actually
*hit* the end of file. READ THE FAQ!!!!!

http://parashift.com/c++-faq-lite/in....html#faq-15.5

Dec 27 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: DJP | last post by:
Hi, I need to read a file programmatically until end of file. My logic is as follows: while(!feof(Fp)) { fgets(readLine,10000,Fp);
3
by: psbasha | last post by:
Hi , When ever we read any data from file ,we read as a single line string ,and we convert the respective field data available in that string based on the data type ( say int,float ). ...
5
by: ramana | last post by:
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...
11
by: itdevries | last post by:
Hi, I'm trying to convert some char data I read from a binary file (using ifstream) to a float type. I've managed to convert the int types but now I need to do the float types as well but it...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.