0

In the following code i get a bug which i cannot get rid of. The exe shows last verse twice. How can I fix this? (file contains text in this format: 1;name ;1;name2 ;2 \n 1;name2 ;1;name3 ;2

void WZ(string nazwa)
{
std::fstream plik;
plik.open( nazwa.c_str() );
while( true )
{
if( plik.good() )
    {
        int oferta;
        string dane;
        int ilosc;
        string koszyk;
        int status;
        int licznik=0;
        for(int a=0; a<5;a++)
        {
            plik>>oferta;
            plik.ignore( numeric_limits < streamsize >::max(), ';' );
            plik>>dane;
            plik.ignore( numeric_limits < streamsize >::max(), ';' );
            plik>>ilosc;
            plik.ignore( numeric_limits < streamsize >::max(), ';' );
            plik>>koszyk;
            plik.ignore( numeric_limits < streamsize >::max(), ';' );
            plik>>status;
            licznik++;
            cout<<oferta<<dane<<ilosc<<koszyk<<status<<"licznik: "<<licznik<<endl;
        }
    }
    else
    {
        break; //zakoñcz wczytywanie danych - wyst¹pi³ jakiœ b³¹d (np. nie ma wiêcej danych w pliku)
    }
}
}
songyuanyao
  • 147,421
  • 15
  • 261
  • 354
saint337
  • 33
  • 1
  • 8
  • 2
    This is a variant of ["don't use `while(!eof)`"](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – user657267 Sep 15 '14 at 09:11

1 Answers1

0

You should check for plik->fail() after reading your variables from the file. EOF isn't asserted until after it is read. The "usable end" of the file, isn't the same as EOF.

Luke Dupin
  • 1,923
  • 17
  • 26