0

I was going through a C++ tutorial that showed how to read from a text file. In the tutorial, it's been mentioned that "while(file)" and "while(!file.eof())" is the same, where file is of ifstream type. But for some reason, I get different results(I added my results in this post). Some help will be really appreciated

int main()
{
    string fileName = "read_text.txt";
    ifstream inFile;
    inFile.open(fileName);

    if(inFile.is_open())
    {
        string line;
        while(inFile)
        {
            getline(inFile, line);
            cout<<line<<endl;
        }

        cout<<endl;
        inFile.clear();
        inFile.seekg(ios::beg);

        while(!inFile.eof())
        {
            getline(inFile, line);
            cout<<line<<endl;
        }
        inFile.close();
    }
    else cout<<"The file failed to open";
    return 0;
}

Images of the text file and the output

D-PUNK-R
  • 25
  • 4
  • 1
    The descriptions of [std::basic_ios::operator bool](https://en.cppreference.com/w/cpp/io/basic_ios/operator_bool) and [std::basic_ios::eof](https://en.cppreference.com/w/cpp/io/basic_ios/eof) are different and don't say that they are equivalent. – t.niese Jan 18 '19 at 10:43
  • 1
    Stay away from that tutorial. If it can't get that right, it's most likely full of other errors. – molbdnilo Jan 18 '19 at 11:43

0 Answers0