Search code examples
c++streamifstream

C++ std::ifstream: check if characters are left to read


Is there a way to check if any characters are left in an ifstream to read and if yes, how can I do this. If you know for sure that this isn't possible, please tell me so.


Solution

  • To get what you're asking about after the edit, you can use the peek() function:

    Given an std::ifstream called f

    if (f && f.peek() == EOF)
        std::cout << "Nothing left to read\n";
    else
        std::cout << "There is something to read or the stream is bad\n";
    

    But keep in mind that this is not a 'more general' question, it is a different question (that is, applying this to your original question would be an error)