I'm using the following method to read the content of a file into a string:
std::ifstream t("file.txt");
std::stringstream buffer;
buffer << t.rdbuf();
std::string data(buffer.str());
But how do I check for I/O errors and ensure that all the content has actually been read?
You can do it the same way you would do it with any other insertion operation:
if (buffer << t.rdbuf())
{
// succeeded
}
If either the extraction from t.rdbuf()
or the insertion to buffer
fails, failbit
will be set on buffer
.
You can use t.good().
You can look description on http://www.cplusplus.com/reference/iostream/ios/good/
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