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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With