The program for this month's C++ users group meeting is one of the shortest programs that we have discussed in quite a while and yet it uses the concepts which are very powerful. The concepts are part of the C++ language and are contained in a series of libraries called the Standard Template Library or STL.
Here's the situation. A friend wanted to find out how many files had been changed since the last time he backed up his system. The Windows operating system keeps a flag with every filename called the archive flag. The goal is to write a program to scan all of the files on a hard drive and to check the archive flag for each file. For those files that have not been backed up, let us make up a list that includes the name, the size of the file, the date and time, and the full path of that filename. That could be a lot of filenames!
The standard template library contains a container class that includes a list object. This program uses this list container to store all of the data records of interest. A typical hard drive has tens of thousands of files. Some even as many as 100,000 files. In in addition, path names can be up to 256 characters long. The STL takes care of the details of allocating memory, indexing, transferring, searching and even sorting. The result is a short and fairly simply program provided that you can grasp how to interface to the STL. This is what we will discuss at the meeting.
/* *************** SAMPLE OUTPUT ************************************************ Autil.cpp 6863 03/13/2002 23:38:19 C:\MSDEV\Projects\Autil\Autil.cpp Autil.dsp 4266 03/13/2002 22:54:16 C:\MSDEV\Projects\Autil\Autil.dsp Autil.dsw 348 03/13/2002 22:54:02 C:\MSDEV\Projects\Autil\Autil.dsw Autil.exe 1658936 03/13/2002 23:38:20 C:\MSDEV\Projects\Autil\Debug\Autil.exe Autil.h 323 03/13/2002 22:54:16 C:\MSDEV\Projects\Autil\Autil.h Autil.ilk 2473808 03/13/2002 23:38:20 C:\MSDEV\Projects\Autil\Debug\Autil.ilk
#include <iostream> #include <iomanip> #include <list> int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; char *p, absPath[_MAX_PATH+1]; list<FILEinfo> files; // Create list based on the FILEINFO structure. list<FILEinfo>::iterator i; FILEinfo x, y; p = _fullpath(absPath, argv[1], _MAX_PATH ); // Expand user input cerr << "Searching from " << p << "... "; process(p, files); // Make a list of all files cerr << endl; // on the systerm. cerr << "Sorting data..."; files.sort(); // User previously defined sort criteria. cerr << endl; cout.setf(ios::left); char buff[26]; for (i = files.begin(); i != files.end(); ++i) // Output the results. { cout << setw(25) << ShortString(buff, (*i).Name, 26); cout << (LPCTSTR) (*i).Data << " "; cout << (LPCTSTR) (*i).FullName << endl; } cout.unsetf(ios::left);