I have a function that loads a file. From this file it has to a get a number and return it. The way I currently do it is to return a bool for the status of the operation (because file opening, reading, etc.. can fail) and get the number as a parameter by reference. It works, but I think it's ugly and doesn't do the name of the function justice. Would there be a better way where I return the int and still convey if the opening/reading of the file failed somehow?
bool get_number_of(const char* file_name, const unsigned size_of, unsigned& number)
{
std::ifstream file(file_name, std::ios::binary|std::ios::in);
// try to open file
if(!file.is_open())
return false;
if(file.tellg() > 0)
{
file.seekg(0, std::ios::end);
number = file.tellg() / sizeof(size_of);
}
else
number = 0;
file.close();
return file.good();
}