/* countfiles
*
* Small example application which counts the number of files (and directories)
* in a specified directory.
*
* It demonstrates the use 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: countfiles /some/directory" << endl ;
return -1 ;
}
string dir (argv[1]) ;
filesystem::file_iterator<> i (dir);
// start with one because we count the root directory, too
int count = 1 ;
int count_dirs = 1 ;
for (; i != i.end (); i++)
{
// As this is an example application, we show how to
// access the internal file_t objects.
const filesystem::file_t& f = *i ;
// The default filesystem::file_t object only provides a method to
// access the filename:
const string& fname = f.getName () ;
// It's easy to check if a file is actually a directory:
if (filesystem::isDirectory (fname))
count_dirs++ ;
count++ ;
}
// compare this with "find | wc -l" and "find -type d | wc -l" ;-)
cout << count << " total files (containing "
<< count_dirs << " directories)" << endl ;
return 0 ;
}
syntax highlighted by Code2HTML, v. 0.9.1