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 (将#修改为@)