#include #include #include #include #include using namespace std; #include "directory.h" // illustrates use of the DirStream and DirEntry classes // and some STL functions struct SizeCompare { bool operator () (const DirEntry& lhs, const DirEntry& rhs) const { return lhs.Size() < rhs.Size(); } }; struct DateCompare { bool operator () (const DirEntry& lhs, const DirEntry& rhs) const { return lhs.GetDate() < rhs.GetDate(); } }; ostream& operator << (ostream& out, const DirEntry& entry) { const int NAME_FIELD = 20; out << setw(NAME_FIELD - entry.Name().length()) << entry.Name() << " " << entry.GetDate() << "\t" << entry.Size(); return out; } int main(int argc, char * argv[]) { DirStream dir; // directory information DirEntry entry; // one entry from a directory vector files; // list of files string dirname = "."; // default is current directory if (argc > 1) // command line argument is dirname { dirname = argv[1]; } dir.open(dirname); if (dir.fail()) { cerr << "could not open directory " << dirname << endl; exit(1); } for(dir.Init(); dir.HasMore(); dir.Next()) { files.push_back(dir.Current()); } copy(files.begin(), files.end(), ostream_iterator(cout,"\n")); cout << "\n\n sorted \n\n"; sort(files.begin(),files.end(),SizeCompare()); copy(files.begin(), files.end(), ostream_iterator(cout,"\n")); return 0; }