微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ofstreamFile << ifstreamFile.rdbuf() 失败,但文件中的文件文本正确写入 ofstreamFile

如何解决ofstreamFile << ifstreamFile.rdbuf() 失败,但文件中的文件文本正确写入 ofstreamFile

std::ifstream inFile;
std::ofstream outFile;
// ...
outFile << inFile.rdbuf();

我正在用 rdbuf 将一个文件写入另一个文件,一切都很好。我决定添加错误检查

std::ifstream inFile;
std::ofstream outFile;
// ...
if(!(outFile << inFile.rdbuf()))
// error message

并且在相同的文件上它一直开始失败,但文本被复制到 outFile 中。但是如果我检查

std::ifstream inFile;
std::ofstream outFile;
// ...
outFile << inFile.rdbuf();
if (inFil2.bad() || !inFile2.good())
  // error message

没有错误消息,程序运行良好,我复制了我的文件

更新: outFile.bad() == false,但 outFile.fail() == true。

failbit is generally set by an operation when the error is related to the internal logic of the operation itself; further operations on the stream may be possible

我真的不明白“操作本身的内部逻辑”是什么意思..也许有人可以解释一下? 尽管文本似乎从一个文件正确复制到另一个文件,但这是一种找到发生了什么以及为什么设置了失败位的方法吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。