PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > while(!FP.eof()) is reading the last data point of the file twice
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
while(!FP.eof()) is reading the last data point of the file twice

Réponse
 
LinkBack Outils de la discussion
Vieux 25/12/2007, 22h57   #1
ramana
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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

  Réponse avec citation
Vieux 26/12/2007, 00h06   #2
Daniel T.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: while(!FP.eof()) is reading the last data point of the file twice

ramana <ramana.dodla@gmail.com> 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


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.
  Réponse avec citation
Vieux 26/12/2007, 00h07   #3
LR
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: while(!FP.eof()) is reading the last data point of the file twice

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;
}
}
}
  Réponse avec citation
Vieux 26/12/2007, 00h49   #4
ramana
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: while(!FP.eof()) is reading the last data point of the file twice


> > //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
  Réponse avec citation
Vieux 26/12/2007, 00h56   #5
ramana
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: while(!FP.eof()) is reading the last data point of the file twice


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

  Réponse avec citation
Vieux 26/12/2007, 03h22   #6
red floyd
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: while(!FP.eof()) is reading the last data point of the file twice

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.
  Réponse avec citation
Vieux 26/12/2007, 12h17   #7
Sachin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: while(!FP.eof()) is reading the last data point of the file twice

On Dec 26, 8:22am, red floyd <no.s...@here.dude> wrote:
> 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.
  Réponse avec citation
Vieux 27/12/2007, 01h35   #8
red floyd
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: while(!FP.eof()) is reading the last data point of the file twice

Sachin wrote:
> On Dec 26, 8:22 am, red floyd <no.s...@here.dude> wrote:
>> 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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h03.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18307 seconds with 16 queries