Let's say I need to perform several read or write operations on a stream and throw an exception if any of them fail. Is there a difference between two ways:
{
std::ifstream ifs("filename");
int i;
std::string s;
long l;
if(!ifs >> i) throw runtime_error("Bad file");
if(!std::getline(ifs, s)) throw runtime_error("Bad file");
if(!ifs >> l) throw runtime_error("Bad file");
ifs >> i;
std::getline(ifs, s);
ifs >> l;
if(!ifs) throw runtime_error("Bad file");
}
If there is no difference, then are there any pitfalls in similar cases that I should know?
source
share