I need help finishing my code for c++17 #include #include #include using namespace std::filesystem; int main() {
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); // 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
Accounting for Decision Making and Control
ISBN: 978-1259564550
9th edition
Authors: Jerold Zimmerman