Write a (POSIX) program that searches for a specified file, and if found, prints
ID: 3712952 • Letter: W
Question
Write a (POSIX) program that searches for a specified file, and if found, prints out the relative path of the file from the current working directory and its time stamp. Your program should take one command line argument, the filename to find. If your executable file is “searchFile” and you run this program from the root directory (“/”) and search for a file named “test.txt”. Your output might be something like the following:
$./searchFile test.txt
Searching for test.txt …
Found test.txt in /home/user/CIS324/
Last Edited: Jun 7 10:30
Explanation / Answer
void SearchDirectory(string directory, string target_File_Name){ DIR *dirp = opendir(directory.c_str()); if (!dirp) { perror(("opendir " + directory).c_str()); return; } struct dirent *dptr; while(dptr = readdir(dirp)){ string file_Name = dptr->d_name; string file_Path = directory + "/" + file_Name; struct stat statStruct; stat(file_Path.c_str(), &statStruct); if( S_ISDIR(statStruct.st_mode) ){ if ( file_Name.compare(".") == 0 || file_Name.compare("..") == 0 ){ continue; } SearchDirectory(file_Path, target_File_Name); } else if( S_ISREG(statStruct.st_mode)){ if( file_Name.compare(target_File_Name) == 0){ coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.