无法打开文件时我应该关闭文件吗?
我应该这样写吗:
std::ifstream file(DATA_PATH);
if (!file.good()) //File doesn't exist
{
//do something
}
else //file exists
{
//do something
file.close();
}
还是我应该写:
std::ifstream file(DATA_PATH);
if (!file.good()) //File doesn't exist
{
//do something
}
else //file exists
{
//do something
}
file.close();
不,没有必要明确地做。(文件)流总是在隐式超出范围时关闭。
a的close()
功能std::iostream()
也是幂等操作,在关闭流(或永远不会成功打开)之后,绝不会损害流状态。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句