当我使用 std::ifstream
时,是否需要手动调用 close()
呢?
例如,在以下代码中:
std::string readContentsOfFile(std::string fileName) {
std::ifstream file(fileName.c_str());
if (file.good()) {
std::stringstream buffer;
buffer << file.rdbuf();
file.close();
return buffer.str();
}
throw std::runtime_exception("file not found");
}
我需要手动调用 file.close()
吗?ifstream
不应该使用RAII关闭文件吗?
close()
来处理错误。 - ks1322