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

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)


You should put the read operation in your while condition:

while(stream >> buffer) {
    ...

That will read until the stream is empty or another error occurs.

...but if you really are trying to read one character at a time, you should read this: Reading a single character from an fstream?