我使用C++ 11以这样的方式复制一个文件:
std::ifstream src(srcPath, std::ios::binary); std::ofstream dst(destinationPath, std::ios::binary); dst << src.rdbuf();
我正在通过以下方式创建新文件:
std::ofstream out(path); out << fileContent; out.close();
在这两种情况下,如何检查操作是否实际成功或失败?
operator bool 是为 ostream& 返回流插入。所以你可以用 if 声明:
operator bool
ostream&
if
if (dst << src.rdbuf()) { // ... } if (out << fileContent) { // ... }