c++filefreopen

How to detect end-of-file when using freopen in C++?


There's feof for fopen. But I am using

freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);

What is the equivalent of feof for this?


Solution

  • It is the same as when the file was opened with fopen: You use feof.

    For example, after freopen("in.txt", "r", stdin);, feof(stdin) will tell you whether you hit the end of "in.txt".

    The usual warning about feof applies: feof does not tell you whether you are at the end of the file now - it tells you whether the last thing you tried to do failed because you got to the end of the file. See: Why is “while ( !feof (file) )” always wrong?