diff --git a/AdvancedCppV2/Presentation/exercises/files/main.cpp b/AdvancedCppV2/Presentation/exercises/files/main.cpp index 7202bde..66f041d 100644 --- a/AdvancedCppV2/Presentation/exercises/files/main.cpp +++ b/AdvancedCppV2/Presentation/exercises/files/main.cpp @@ -1,5 +1,7 @@ #include #include +#include +#include namespace fs = std::filesystem; @@ -56,26 +58,17 @@ int main() { std::cout << "File write time is " << std::asctime(std::localtime(&time)); }; - fs::path last_mod; - fs::path longest; + std::vector paths; 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(); - } + paths.push_back(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)); + + std::sort(paths.begin(), paths.end(), [](fs::path a, fs::path b) {return fs::last_write_time(a) > fs::last_write_time(b);}); + + std::cout << "Last modified file is: " << paths.front() << '\n'; + print_last_write_time(std::filesystem::last_write_time(paths.front())); + std::cout << "files which were longest unmodified: " << paths.back() << std::endl; + print_last_write_time(std::filesystem::last_write_time(paths.back())); }