6

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?

acraig5075
  • 10,588
  • 3
  • 31
  • 50
CodeMonkey
  • 11,196
  • 30
  • 112
  • 203

1 Answers1

7

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)
    { // ... }
acraig5075
  • 10,588
  • 3
  • 31
  • 50