Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The task of this assignment is to conform to the following: If no command line arguments are passed to this program, then the path used
The task of this assignment is to conform to the following:
If no command line arguments are passed to this program, then the path used is the current directory, ie
If a single command line argument is passed to this program, then the path used is that, ie argv
The regular expression to be used to check for matching file names is :alnum:txt with
regexconstants::ECMAScript regexconstants::icase.
If a std::filesystem::filesystemerror exception occurs at anytime during processing, the following must be output before exiting with an exit status of : "EXCEPTION: follows by ewhatwhere e is the caught exception followed by a newline character all written to std::clog.
If any other exception occurs at anytime during processing, the following must be output to std::clog: "EXCEPTION: unknown
and then the program must exit with an exit status of
The program must search the path nonrecursively and output all files that are between hours and days ieh old based on the current time. For any file that matches such, "FOUND: followed by that file's std::filesystem::path, a space, and then that file's time simply output the object returned from std::filesystem::directoryentry's lastwritetime member function
Tips and Comments
To assist with this assignment note the following:
Consider using these #include files: and
Refer to your textbooks for help with these. As a reference only not as a tutorial cppreference.com is also useful.
Recall from COMPSystems Programming how command line arguments are passed in to mainIf more than one argument is passed in don't worry about the extra arguments only process the first argument.
When storing the path to start searching for filesm, use std::filesystem::path.
As it can get tedious writing std::filesystem and std::chrono everyone consider using these namespace aliases:
namespace fs std::filesystem;
namespace chr std::chrono;
You are only permitted to use two "using namespace" statements in your program:
using namespace std;
using namespace std::literals::chronoliterals;
Place these at file scope AFTER your #include files.
To get the current FILESYSTEM time, use:
auto const cuttime chr::fileclock::now;
Then compute the two times you need storing these values in variables so you can use them later:
curtime h
curtime h
The rest of the code in main will be all in a try catch block.
Store a fs::path holding the path passed in from the command line.
Use a rangebased for loop to iterate over the current directory only ie don't do this recursively!
Pass to the constructor of the iterator used fs::directoryoptions::skippermissiondenied
Inside the for loop declare a static std::regex variable passing the aforementioned regular expression and the std::regexconstants to that variable's constructor.
ASIDE: If the std::regex variable is NOT inside the loop then it need not be static. Technically inside the loop it doesn't need to be static either. What is the difference? The value of the std::regex variable never changes but std::regex argument string are actually compiled in the constructor code and that takes time. In this loop you are reusing the std::regex variable multiple times and the regular expression is not changing, thus, one shouldn't be recompiling the regular expression each time unnecessarily. Making it static causes it to be only initialized once and so the compilation occurs only once and since its regular expression is only one string always using static works
Determine if there is a match ie use std::smatch which matches std::string objects and call std::regexmatch passing in your fs::path variable's pathfilenamestring as the string to match and the std::regex static variable you declared earlier. When there is a match get that file's lastwritetime and check that such is between the two times you calculated earlier. If it is then output such to the screen with "FOUND: etc. as detailed above.
NOTE: A number of persons have asked about smatch as it is not being used in this assignment. Correct we are just using the return value of regexmatch Normally with regexmatch one makes use of smatch. Thus if you don't use smatch in your code, no worries.
Sample Program Run
After compiling the code, eg
gstdcWall Wextra Woldstylecast Werror Omarchnative o asoln.exe asoln.cxx
and determining the date, eg the date command: date CymdHM
One can then create some files with specific dates within and outside the range and test the program, eg like this:
mkdir p rundir
for ext in txt txT tXt tXT Txt TxT TXt TXT ; do
touch t rundirnotokay$ext ;
touch t rundirokay$ext ;
touch t rundirokay$ext ;
touch t rundirnotokay$ext ;
done
asoln.exe
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started