Question: I need help finishing my code for c++17 #include #include #include using namespace std::filesystem; int main() { const std::filesystem::path from{/nonexistent1/a}, to{/nonexistent2/b}; try { std::filesystem::copy_file(from, to);

I need help finishing my code for c++17

#include
#include
#include

using namespace std::filesystem;

int main()
{
const std::filesystem::path from{"/nonexistent1/a"}, to{"/nonexistent2/b"};
try {
std::filesystem::copy_file(from, to); // throws: files do not exist
}
catch(std::filesystem::filesystem_error const& ex) {
std::cout
<< "what(): " << ex.what() << ''
<< "path1(): " << ex.path1() << ''
<< "path2(): " << ex.path2() << ''
<< "code().value(): " << ex.code().value() << ''
<< "code().message(): " << ex.code().message() << ''
<< "code().category(): " << ex.code().category().name() << '';
}

// All functions have non-throwing equivalents
std::error_code ec;
std::filesystem::copy_file(from, to, ec); // does not throw
std::cout << "non-throwing form sets error_code: " << ec.message() << '';
}

Here are the requirements:

Read a filename from the user

· Create a path from the string (just assign the string to a path variable)

· Check if the path exists using bool exists( const std::filesystem::path & p );

(1) If not, output a message to indicate that

· Remove the file using bool remove( const std::filesystem::path & p );

· Just put this in an infinite loop so you can try different scenarios

· Here are 3 scenarios to try – results may vary depending on the compiler and platform:

(1) The file does not exist

(2) The file exists and is not opened by any process

(3) The file exists, but it is currently opened by another process

This should be the output (user input in bold):

Enter a file name to remove:notafile

"notafile" does not exist

Enter a file name to remove:erasethis.txt

Enter a file name to remove:tmp.txt

what(): remove: The process cannot access the file because it is being used by another process.: "tmp.txt"

path1(): "tmp.txt"

code().value(): 32

code().message(): The process cannot access the file because it is being used by another process.

code().category(): system

Step by Step Solution

3.38 Rating (164 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Here is the modified code to fulfill the requirements you have ment... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Computer Engineering Questions!