boost::filesystem directory_iterator: how to get the last file of a directory ?

Hi boost-users, I am using a recursvive function for iterating over a starting path. When i finally arrive in a directory that contains no subdirectory (what i call a "final" directory), i want to retrieve the first and the last file of this directory. I succeed in retrieving the first file of the final directory (see code snippet below), But i do not know how to retireve the last one using the same directory iterator. Can someone think of an elegant solution ? ------------------ CODE BEGIN ------- namespace fs = boost::filesystem; void my_function (const fs::path &ph) { bool firstFileOfFinalDir = false; if (fs::is_directory(ph)) { fs::directory_iterator end_iter; for (fs::directory_iterator dir_itr(ph); dir_itr != end_iter; ++dir_itr) { if (fs::is_directory(dir_itr->status())) { // Recursive call my_function(fs::path(dir_itr->path())); } else { // We are in a "final" directory if (fs::is_regular_file( dir_itr->status())) { if (firstFileOfFinalDir) { // Do something with the first file of the final directory firstFileOfFinalDir = false; } else { // My problem : how to check if // dir_itr == (end_iter - 1) // i.e. if it is the last valid value ? } } } } } } ------------------ CODE END ------- Thank you in advance for your help, Florent Be a part of filmmaking's exciting future today by registering your intent to purchase on: www.arridigital.com Arnold & Richter Cine Technik GmbH & Co. Betriebs KG Sitz: Munchen - Registergericht: Amtsgericht Munchen - Handelsregisternummer: HRA 57918 Personlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH Sitz: Munchen - Registergericht: Amtsgericht Munchen - Handelsregisternummer: HRB 54477 Geschaftsfuhrer: Franz Kraus; Dr. Martin Prillmann

Zitat von "Vial, Florent"
Hi boost-users,
I am using a recursvive function for iterating over a starting path. When i finally arrive in a directory that contains no subdirectory (what i call a "final" directory), i want to retrieve the first and the last file of this directory.
define "first" and "last". directory iterators are single-pass input iterators, meaning that they represent no particular order. if you iterate over the same directory 2 times, you might retrieve the entries in a different order. you could e.g. copy the entries to a std::vector using std::copy and then sort them, choose first and last, etc.
participants (2)
-
strasser@uni-bremen.de
-
Vial, Florent