/* backdir
*
* Small example application which lists the first 10 files of a directory and
* its subdirectories and then iterates back to the first file.
*
* It demonstrates how to use the operator-- of the filesystem::file_iterator.
*/
/* Note: Do not include config.h in your own programs. See README. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <fs++/file_iterator.h>
#include <iostream>
#include <string>
using namespace std ;
int
main (int argc, char** argv)
{
if (argc != 2)
{
cerr << "Usage: backdir /some/directory" << endl ;
return -1 ;
}
string dir (argv[1]) ;
filesystem::file_iterator<> i (dir);
const int maxcount = 10 ;
for (int c = 0; (i != i.end ()) && (c < maxcount); c++, i++)
{
// This calls a special output operator for file_t ;-)
cout << c << " " << (*i) << endl ;
}
if (i == i.end ())
{
cerr << endl << "Sorry, directory has less than " << maxcount
<< " files." << endl ;
return 1 ;
}
cout << "---" << endl ;
i-- ;
for (int c = (maxcount - 1) ; (i != i.end ()) && (c >= 0); c--, i--)
{
cout << c << " " << (*i) << endl ;
}
if (i != i.end ())
{
// This should not occure in this scenario ;-)
cerr << "Strange, we are not yet at the beginning..." << endl ;
return 1 ;
}
return 0 ;
}
syntax highlighted by Code2HTML, v. 0.9.1