【发布时间】:2010-10-19 09:07:59
【问题描述】:
使用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 来关闭文件吗?
【问题讨论】: