如何在成功或失败的情况下返回 std::copy 的值? [英] How to return value of std::copy in case of success or failure?

查看:37
本文介绍了如何在成功或失败的情况下返回 std::copy 的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 std::copystd::deque 中的对象复制到文件中.代码工作正常,但我需要检查复制是否成功,因此我需要设置标志或抛出异常.

I am using std::copy to copy the objects in std::deque to a file. The code is working fine but I need to check if copying was successful and accordingly I need to set the flag or else throw exception.

我用谷歌搜索但找不到如何检查 std::copy 是否已成功将值复制到文件中的解决方案.

I have googled but could not find the solution how to check if std::copy has successfully copied the values into the file.

有人可以点亮它吗.

推荐答案

如果写入文件失败,则文件流的错误标志将被设置 - 您可以在复制后检查这些,或调用 exceptions 成员函数首先使其在出错时抛出异常.如果其他任何操作失败,则会抛出异常.

If writing to the file fails, then the file stream's error flags will be set - you can either check these after the copy, or call the exceptions member function first to make it throw an exception on error. If anything else fails, then an exception will be thrown.

这篇关于如何在成功或失败的情况下返回 std::copy 的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆