C++ 我需要关闭 std::fstream 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4802494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
do I need to close a std::fstream?
提问by Tobias Langner
Possible Duplicate:
Do I need to manually close a ifstream?
可能重复:
我需要手动关闭 ifstream 吗?
Do I need to call fstream.close()
or is fstream
a proper RAII object that closes the stream on destruction?
我是否需要调用fstream.close()
或者是fstream
一个适当的 RAII 对象来关闭销毁流?
I have a local std::ofstream
object inside a method. Can I assume that the file is always closed after exiting this method without calling close? I could not find documentation of the destructor.
我std::ofstream
在方法中有一个本地对象。我可以假设在退出此方法后文件总是关闭而不调用 close 吗?我找不到析构函数的文档。
回答by Konrad Rudolph
I think the previous answers are misleading.
我认为以前的答案具有误导性。
fstream
isa proper RAII object, it doesclose automatically at the end of the scope, and there is absolutely no need whatsoeverto call close
manually when closing at the end of the scope is sufficient.
fstream
是一个适当的 RAII 对象,它会在范围结束时自动关闭,并且在范围结束时关闭就足够了,绝对不需要close
手动调用。
In particular, it's not a “best practice” and it's not necessary to flush the output.
特别是,这不是“最佳实践”,也没有必要刷新输出。
And while Drakosha is right that calling close
gives you the possibility to check the fail bit of the stream, nobody does that, anyway.
虽然 Drakosha 是正确的,调用close
让你有可能检查流的失败位,但无论如何没有人这样做。
In an ideal world, one would simply call stream.exception(ios::failbit)
beforehand and handle the exception that is thrown in an fstream
's destructor. But unfortunately exceptions in destructors are a broken concept in C++ so that's not a good idea.
在理想的世界中,人们只需stream.exception(ios::failbit)
预先调用并处理在fstream
's 析构函数中抛出的异常。但不幸的是,析构函数中的异常在 C++ 中是一个破碎的概念,所以这不是一个好主意。
So ifyou want to check the success of closing a file, do it manually (but only then).
因此,如果您想检查关闭文件是否成功,请手动执行(但仅限于那时)。
回答by Drakosha
To append to Amy Lee's answer, it's better to do it manually because this way you can check for errors too.
要附加到 Amy Lee 的答案,最好手动进行,因为这样您也可以检查错误。
BTW, according to "close" manpage:
顺便说一句,根据“关闭”联机帮助页:
Not checking the return value of close() is a common but nevertheless serious programming error. It is quite possible that errors on a previous write(2) operation are first reported at the final close(). Not checking the return value when closing the file may lead to silent loss of data. This can especially be observed with NFS and with disk quota.
A successful close does not guarantee that the data has been successfully saved to disk, as the kernel defers writes. It is not common for a filesystem to flush the buffers when the stream is closed. If you need to be sure that the data is physically stored use fsync(2). (It will depend on the disk hardware at this point.)
不检查 close() 的返回值是一个常见但仍然严重的编程错误。很有可能在最后一次 close() 时首先报告先前 write(2) 操作的错误。关闭文件时不检查返回值可能会导致数据无声丢失。这尤其可以通过 NFS 和磁盘配额观察到。
成功关闭并不能保证数据已成功保存到磁盘,因为内核会延迟写入。当流关闭时,文件系统刷新缓冲区并不常见。如果您需要确保数据是物理存储的,请使用 fsync(2)。(此时将取决于磁盘硬件。)
回答by Amy Lee
I think it's a good practice to close your fstream, cause you need to flush the buffer, that what i've been told
我认为关闭 fstream 是一个很好的做法,因为您需要刷新缓冲区,这是我被告知的