diff --git a/AdvancedCppV2/Presentation/exercises/files/main.cpp b/AdvancedCppV2/Presentation/exercises/files/main.cpp index 833c662..7202bde 100644 --- a/AdvancedCppV2/Presentation/exercises/files/main.cpp +++ b/AdvancedCppV2/Presentation/exercises/files/main.cpp @@ -50,15 +50,32 @@ int main() { printPermissionsForDir(); // Part 2 unncoment later - /* auto print_last_write_time = [](std::filesystem::file_time_type const& ftime) { std::time_t time = std::chrono::system_clock::to_time_t( std::chrono::file_clock::to_sys(ftime)); std::cout << "File write time is " << std::asctime(std::localtime(&time)); }; - std::cout << "Last modified file is: " << *last_modify << '\n'; - print_last_write_time(std::filesystem::last_write_time(*last_modify)); - std::cout << "files which were longest unmodified: \n"; - // Print it here - */ + + fs::path last_mod; + fs::path longest; + for (const auto& entry : fs::directory_iterator(fs::current_path().string() + "/project")) { + if (fs::is_regular_file(entry.path())) { + if(last_mod == ""){ + last_mod = entry.path(); + longest = entry.path(); + continue; + } + + if(fs::last_write_time(last_mod) < fs::last_write_time(entry.path())){ + last_mod = entry.path(); + } + if(fs::last_write_time(longest) > fs::last_write_time(entry.path())){ + longest = entry.path(); + } + } + } + std::cout << "Last modified file is: " << last_mod << '\n'; + print_last_write_time(std::filesystem::last_write_time(last_mod)); + std::cout << "files which were longest unmodified: " << longest << std::endl; + print_last_write_time(std::filesystem::last_write_time(longest)); }