2

I came across this code which deals with a simple reading of a file and displaying its contents.

#include &ltiostream&gt
#include &ltfstream&gt

int main()
{   
    ...
    fstream file;
    file.open("TEXT.txt", ios::in);
    file.seekg(0);
    while(file) //does file returns 0 when eof is reached?
    {
        file.get(ch);
        cout &lt&lt ch;
    }
    return 0;
}

My question is how does while (file) realizes that the end of the file has been reached.

  • Alex's Answer below true... For more information I would suggest looking at this tutorial on Stream States http://www.learncpp.com/cpp-tutorial/135-stream-states-and-input-validation/ – silvergasp Oct 04 '15 at 06:42
  • 1
    Watch out with the above code. `while (file)` enters the body of the loop if the stream is not in an error state, including end of file. This is good, but `file.get(ch);` may still fail and this result is not being tested. A better implementation would be `while(file.get(ch))` because that's going to test after trying to read. – user4581301 Oct 04 '15 at 06:49
  • @user4581301 The long explanation is here: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – πάντα ῥεῖ Oct 04 '15 at 06:53
  • Your loop is backwards, if `file.get` fails it goes on to `cout << ch` anyway – M.M Oct 04 '15 at 07:04
  • @M.M How do I put it then? –  Oct 04 '15 at 07:26
  • while ( file.get(ch) ) cout << ch; – M.M Oct 04 '15 at 08:00

2 Answers2

5

The stream implements a boolean cast operator that will return true if the stream is still good or false if there's an error or eof.

Darklighter
  • 1,884
  • 1
  • 13
  • 21
Alex Fitzpatrick
  • 643
  • 5
  • 10
  • Is it equivalent to `while(fin.eof())` ? –  Oct 04 '15 at 06:55
  • 1
    @NooB No. `while(fin)` catches more error conditions than just EOF. – user4581301 Oct 04 '15 at 06:56
  • But I should add that, neither one does what you want. Both test for error before operating on the stream and potentially raising an error. Read the the link posted by @πάνταῥεῖ above for the straight dope. – user4581301 Oct 04 '15 at 07:06
  • @user4581301 great understood, also I know its a bit off topic but 1. Is it necessary to close a file explicitly like `fin.close();` for a small program and 2. Whether all files are automatically closed when a program terminates? –  Oct 04 '15 at 07:11
  • @NooB 1) Depends on when you want the file closed. If you want it closed immediately, call `close`; otherwise, just let the stream go out of scope and the destructor will take care of it for you. 2) two things help you here: The file stream destructor will fire under normal circumstances and close the file. If for some reason you don't call the destructor, called `exit` or `new`ed the stream and never called `delete` most likely, the system should take care of it for you. – user4581301 Oct 04 '15 at 07:29
0

As others have pointed out you should really use something like

char ch;
while (file.get(ch)) {
    ...
}
Zaid Khan
  • 686
  • 2
  • 9
  • 21
  • 2
    although this helps improve the code, it doesn’t answer the question, both current answers combined would be perfect – Darklighter Oct 04 '15 at 15:00
  • Yeah it was originally intended to supplement Alex's answer as well as πάντα ῥεῖ's suggestion. – Zaid Khan Oct 06 '15 at 14:25