我需要关闭 std::fstream 吗?

发布于 2024-10-14 00:03:39 字数 340 浏览 7 评论 0原文

可能的重复:
我需要手动关闭 ifstream 吗?

我需要调用 fstream.close() 或 fstream 是一个在销毁时关闭流的正确 RAII 对象吗?

我在方法中有一个本地 std::ofstream 对象。我可以假设退出此方法后文件始终关闭而不调用 close 吗?我找不到析构函数的文档。

Possible Duplicate:
Do I need to manually close a ifstream?

Do I need to call fstream.close() or is fstream a proper RAII object that closes the stream on destruction?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疏忽 2024-10-21 00:03:39

我认为关闭 fstream 是一个很好的做法,因为您需要刷新缓冲区,这是我被告知的

I think it's a good practice to close your fstream, cause you need to flush the buffer, that what i've been told

十秒萌定你 2024-10-21 00:03:39

要附加到艾米·李的答案,最好手动执行,因为这样您也可以检查错误。

顺便说一句,根据 "close" 手册页

不检查返回值
close() 是一个常见的方法,但仍然
严重的编程错误。这是相当
可能是之前的错误
首先报告 write(2) 操作
在最后的 close() 处。不检查
关闭文件时可能返​​回值
导致数据无声丢失。这个可以
尤其是使用 NFS 进行观察
有磁盘配额。

成功关闭并不能保证
表明数据已成功
保存到磁盘
,因为内核会延迟
写道。对于一个
文件系统刷新缓冲区时
该流已关闭。如果您需要
确保数据是物理的
存储使用 fsync(2)。 (这将取决于
此时在磁盘硬件上。)

To append to Amy Lee's answer, it's better to do it manually because this way you can check for errors too.

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.)

七色彩虹 2024-10-21 00:03:39

我认为之前的答案有误导性。

fstream 一个正确的 RAII 对象,它确实在作用域结束时自动关闭,并且绝对没有任何必要< /em> 在范围末尾关闭时手动调用 close 就足够了。

特别是,这不是“最佳实践”,也没有必要刷新输出。

虽然 Drakosha 是正确的,调用 close 让您可以检查流的失败位,但无论如何,没有人这样做。

在理想的情况下,只需事先调用stream.exceptions(ios::failbit)并处理fstream析构函数中引发的异常。但不幸的是,析构函数中的异常在 C++ 中是一个被破坏的概念,所以这不是一个好主意。

因此,如果您想检查关闭文件是否成功,请手动执行(但仅在此时)。

I think the previous answers are misleading.

fstream is a proper RAII object, it does 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.

And while Drakosha is right that calling close gives you the possibility to check the fail bit of the stream, nobody does that, anyway.

In an ideal world, one would simply call stream.exceptions(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.

So if you want to check the success of closing a file, do it manually (but only then).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文