1

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.

Abhishek Mane
  • 619
  • 7
  • 20
  • 1
    UNRELATED: Loved your file name `ben_stokes.txt` – ShahzadIftikhar Aug 15 '21 at 15:37
  • You could read the entire file using a single `for` statement: https://github.com/another-ghasem/examples/blob/master/sub_examples/file_string_to_number/source/main.cpp#L54 – Ghasem Ramezani Aug 15 '21 at 15:40
  • 2
    Are you sure it's even opening the file? There's no error checking on that and `eof()` isn't going to check the other flags. See also: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – chris Aug 15 '21 at 15:41
  • I suggest that you analyze your program line by line carefully checking which flags are set and which are checked. – Evg Aug 15 '21 at 16:38
  • @ShahzadIftikhar hey we are from neighbouring countries shall we connect on whatsapp what do you think ? – Abhishek Mane Aug 18 '21 at 14:02

1 Answers1

2

Calling clear will reset the eof flag, so the condition of the while-loop will always evaluate to true. Call clear before getline and the code will work.

Yun
  • 3,056
  • 6
  • 9
  • 28