I am using C++ 11 to copy a file this way:
std::ifstream src(srcPath, std::ios::binary);
std::ofstream dst(destinationPath, std::ios::binary);
dst << src.rdbuf();
I am creating a new file this way:
std::ofstream out(path);
out << fileContent;
out.close();
In both cases, how do I check if the operation actually succeeded or if it failed?
operator bool is defined for the ostream& return of the stream insertion. So you can test for success with an if statement:
if (dst << src.rdbuf())
{ // ... }
if (out << fileContent)
{ // ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With