ben_stokes.txt
The great Irish sports writer Con Houlihan used to say that every team should have a redhead.
And it's true that Ben Stokes' combative nature, allied to his powerful frame and outrageous talent,
lifted England to another level. Never was that more true than when he secured his place in English
cricket history with an indefatigable batting display in the 2019 World Cup final.
Code-1
#include<iostream>
#include<fstream>
int main()
{
std::ifstream fin;
char str[40];
int i=1;
fin.open("ben_stokes.txt", std::ios::in);
while(!fin.eof())
{
fin.getline(str,39,'\n');
fin.clear();
std::cout<<str;
}
fin.close();
}
Output:
The great Irish sports writer Con Houlihan used to say that every team should have a redhead.And it's true that Ben Stokes' combative nature, allied to his powerful frame and outrageous talent,lifted England to another level. Never was that more true than when he secured his place in Englishcricket history with an indefatigable batting display in the 2019 World Cup final._
Just cursor blink program never ends. So I added just extra character !
in code to check what is happening.
Code-2
#include<iostream>
#include<fstream>
int main()
{
std::ifstream fin;
char str[40];
int i=1;
fin.open("ben_stokes.txt", std::ios::in);
while(!fin.eof())
{
fin.getline(str,39,'\n');
fin.clear();
std::cout<<str<<'!';
}
fin.close();
}
Output
The great Irish sports writer Con Houl!ihan used to say that every team shoul!d have a redhead.!And it's true that Ben Stokes' combati!ve nature, allied to his powerful fram!e and outrageous talent,!lifted England to another level. Never! was that more true than when he secur!ed his place in English!cricket history with an indefatigable !batting display in the 2019 World Cup!final.!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!... (upto infinite).
I checked other Questions on stackoverflow related to this. I get that this happens due to fin.eof()
. It just checks now EOF
is occurred or not it doesn't checks next read is EOF
or not ? so we come into loop and reads EOF
so eof-bit and fail-bit set right ? Then why it doesn't come out of loop on next iteration as eof-bit set.