fstream是 a proper RAII object, it 是的 close automatically at the end of the scope, and there is absolutely no need whatsoever to call close manually when closing at the end of the scope is sufficient.
In particular, it’s not a “best practice” and it’s not necessary to flush the output.
虽然 Drakosha 是对的,调用 close可以让您检查流的故障位,但是没有人这样做。
在理想情况下,只需事先调用 stream.exceptions(ios::failbit)并处理在 fstream的析构函数中抛出的异常。但不幸的是,析构函数中的异常在 C + + 中是一个破碎的概念,所以这不是一个好主意。