Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if file copy and writing was successful

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?

like image 679
Yonatan Nir Avatar asked Jul 24 '18 06:07

Yonatan Nir


1 Answers

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)
    { // ... }
like image 188
acraig5075 Avatar answered Sep 27 '22 21:09

acraig5075