Modified the compile program to output the running result in a file name named ". txt;
The running result is changed to the tree output mode.
# Include <iostream>
# Include <iomanip>
# Include <fstream>
# Include <cstdio>
# Include <stack>
# Include <windows. h>
Using namespace STD;
/*
* Traverse all files and folders in lpszpath and display the content in sequence.
*/
/*
* If a folder is scanned, it is saved to the dirs queue and the name is displayed,
* If a file is scanned, its name is displayed;
* After the current folder is processed, process the next level folder.
* Column.
*/
Void function (maid, ostream & out)
{
// Start searching;
Stack <tchar *> dirs;
Stack <int> dirdepth;
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 you find
{
If (curdepth> 1)
Out <"";
For (INT I = 1; I <curdepth-1; ++ I)
Out <'|' <"";
Out <'+' <curdir <Endl;
Do
{
If (WFD. cfilename [0] = '.')
Continue; // filter the "." ".." Directories
If (WFD. dwfileattributes & file_attribute_directory)
{
Wsprintf (szfile, "% S // % s", curdir, WFD. cfilename );
// Function (szfile); // If a directory is found, go to this directory for Recursion
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 <curdepth; ++ I)
Out <'|' <"";
Out <WFD. cfilename <Endl;
}
} While (findnextfile (hfind, & WFD ));
} // If
Delete [] curdir;
Findclose (hfind); // close the search handle
} // ()
}
Int main (INT argc, char * argv [])
{
Ofstream fout ("traversal result .txt ");
If (argc <= 1)
{
Cerr <Endl <"folder traversal, enter the path :";
Tchar path [max_path];
Cin> path;
Function (path, fout );
}
Else
{
Function (argv [1], fout );
}
Return 0;
}