Changed the program so that it can output the result of the file name "traversal result .txt" in the current path; the operation result is changed to the tree output mode.
#include
#include
Using namespace std;
/ * * Traverse all files and folders under LPSZPath and display them in order. * // ** If you scan to the folder, save it in the DIRS queue, and display the name, * If you scan to the file, Then there is a name; * After the current folder is processed, the next level folder is handled, and the next level folder is obtained from the team * column. * /
Void function (LPCTSTR LPSZPATH, OSTREAM & OUT) {// Start Find; Stack
TCHAR * TMP = New Tchar [LSTRLEN (LPSZPATH) 1]; LSTRCPY (TMP, LPSZPATH);
IF (TMP [LSTRLEN (TMP) -1] == '//') TMP [LSTRLEN (TMP) -1] = '/ 0';
TCHAR SZFIND [MAX_PATH * 2]; tchar szfile [max_path * 2]; tchar * curdir; int curdepth = 1; // The depth of the current folder
DIRS.PUSH (TMP); DIRDepth.push (CurDepth);
For (;! DIRS.EMPTY (); {curdir = dirs.top (); curdepth = DIRDEPTH.TOP (); DIRS.POP (); DIRDepth.pop ();
LSTRCPY (SZFIND, CURDIR); LSTRCAT (SZFIND, "//*.*"); // find all files WIN32_FIND_DATA WFD;
Handle Hfind = FindfirstFile (Szfind, & WFD);
IF (HFIND! = INVALID_HANDLE_VALUE) // If {IF (CurDepth> 1) OUT << ""; for (int i = 1; I Do { IF (wfd.cfilename [0] == '.') Continue; // Filter "." ".." two directories IF (wfd.dwfileAttributes & file_attribute_directory) {WSPRINTF (SZFILE, "% S / / /% S", Curdir, Wfd.cfileName; // Function (SZFILE); // If the directory is found, enter this directory to recurrent TCHAR * TMP = New Tchar [lstrlen (SZFILE) 2]; LSTRCPY (TMP, SZFILE); DIRS.PUSH (TMP); DIRDepth.push (CurDepth 1);} else {// Output file name OUT << "; for (int i = 1; i DELETE [] Curdir; Findclose (HFIND); // Turn off the handle } // for ()} INT main (int Argc, char * argv []) {OFStream Fout ("Traversal Results .txt"); if (argc <= 1) {CERR << Endl << "folder traversal, please enter the path:"; tchar Path [MAX_PATH]; CIN >> path; function (path, funt);} else {function (argv [1], fout);} Return 0;}