在 c++ 中复制流时出错使用 rdbuf() 和运算符 <<

发布于 2024-12-19 07:44:13 字数 606 浏览 1 评论 0 原文

“标准”提供的复制文件的方法,例如通过 c++ 是。

ifstream ins;
ofstream out s;
// I've omitted the opening of those stream, since it is not important for the following question.

s << ins.rdbuf();

现在的问题是上面的行可能会失败,并且在某些情况下,尤其是我将要讨论的情况,我发现无法知道出了什么问题。

例如,假设输入流实际上是远程 NAS 计算机上的一个大文件。 现在,可能存在连接错误,导致文件句柄无效。 为了模拟这个,我使用了一个大文件,在调试器中我停在这一行,找到句柄,然后继续调试器,然后强制关闭文件句柄(通过 sysinternals 套件的进程资源管理器)。

结果是:该行完成,文件没有被正确复制,有时根本没有复制,有时仅关闭文件的一部分。 与运算符 << 和 rdbuf() 的文档相矛盾的是,没有设置错误/失败状态,也没有抛出异常。

我唯一注意到的是,当尝试关闭输入流时,会引发异常。

有人有同样的现象吗?! 有没有办法检查/测试这个是否正确完成?

问候,

The "standard" offered way to copy files, for example through c++ is.

ifstream ins;
ofstream out s;
// I've omitted the opening of those stream, since it is not important for the following question.

s << ins.rdbuf();

Now, the problem is that the above line may fail, and in some cases, especially the one I'll talk about, there is no way I found to know that something went wrong.

Assume the input stream is actually a large file sitting on remote NAS machine, for example.
Now, there may be connetion errors which will cause the file handle to be invalid.
Trying to simulate this, I used a large file, in debugger I stopped on this line, found the handle, then continued the debugger and then closed the file handle forcibly (through process-explorer of the sysinternals suite).

The result is: the line finished, the file is not being copied correctly, sometimes it is not copied at all, sometimes only part of the file is being closed.
In contradiction to the documentation of the operator<<, and the rdbuf(), there is no setting of bad/fail state, and no exception is being thrown.

The only thing I could notice, is that when trying to close the input-stream, an exception is being raised.

Did anyone so the same phenomenon ?!
Is there anyway to check/test this for correct completion ?

Regards,

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

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

发布评论

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

评论(1

撧情箌佬 2024-12-26 07:44:13

众所周知,流在错误报告方面非常糟糕。在这个
在这种情况下,您所能做的就是检查输出流上的 badbit ;这
标准要求 << 运算符吸收所有异常,设置
badbit(如果发生)。显然,这并没有告诉你
错误是由于输入错误还是输出错误造成的。

通常,如果您使用的是大多数
编译器,无论如何输入都不会报告错误。对于
大多数情况下,除非近年来情况发生变化,否则实施
filebuf 的处理输入错误的方式与处理文件结尾完全一样。

如果您需要更好的错误报告,我认为您会陷入困境
实现你自己的streambuf(并不是非常困难),
跟踪各种错误;然后你可以询问streambuf
传输后收到什么错误。

Streams are notoriously bad with regards to error reporting. In this
case, all you can do is check for badbit on the output stream; the
standard requires the << operators to absorb all exceptions, setting
the badbit if they occur. And obviously, this doesn't tell you
whether the error was due to an error on input or an error on output.

Typically, if you're using the implementations which come with most
compilers, there won't be an error reported on input anyway. For the
most part, unless things have changed in recent years, implementations
of filebuf treat errors on input exactly like end of file.

If you need better error reporting, I think you're stuck with
implementing your own streambuf (not really very difficult), which
keeps track of the various errors; you can then ask the streambuf
after the transfer what errors it received.

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