The example of this article is similar to traverse a folder and then get a list of files under the folder , you can switch the file directory, is used in our team to write a simple FTP server to the client to display a small plug-in, in short, should not be a single take out what content, A number of Windows APIs were invoked.
Instance code:
#include <iostream> #include <stdlib.h> #include <windows.h> #include <fstream> #include <
stdio.h> #include <vector> #include <string> #pragma comment (lib, "Winmm.lib") using namespace std;
void MainMenu () {printf ("Select operation \ n");
printf ("1. Display all files in the current folder \ n");
printf ("2. Return to previous level \ n");
printf ("3. Enter folder \ n");
printf ("4. Enter the specified folder \ n");
printf ("5. exit \ n");
} void Showfilelist (string filename) {Win32_find_dataa p;
vector<string> filelist;
HANDLE h = Findfirstfilea (Filename.c_str (), &p);
Filelist.push_back (P.cfilename);
while (Findnextfilea (h, &p)) {filelist.push_back (p.cfilename);
if (filelist.back () = "." | | filelist.back () = = "...")
{Filelist.pop_back ();
for (int i = 0; i < filelist.size (); i++) {cout << filelist[i] << Endl;
} void Showlastfilelist (String & filepath) {string filepath2 = filepath; string tmp = "..
/";
TMP + = Filepath2;
filepath = tmp; ShowfilElist (TMP);
} void Showselectfilelist (String filepath) {string yourchoose;
Cin >> Yourchoose;
Yourchoose + = '/';
string filepath2 = filepath;
Yourchoose + = Filepath2;
Showfilelist (Yourchoose);
} void Case4 (String filepath) {string filename;
CIN >> filename;
filename = = '/';
filename = + filepath;
Showfilelist (filename);
int main () {string filepath;
filepath = "*.*";
string filePath = FilePath;
while (1) {System ("CLS");
MainMenu ();
int n;
CIN >> N;
Switch (n) {case 1:system ("CLS");
Showfilelist (FilePath);
System ("pause");
Break
Case 2:system ("CLS");
Showlastfilelist (FilePath);
System ("pause");
Break
Case 3:system ("CLS");
Showselectfilelist (FilePath);
System ("pause");
Break
Case 4:system ("CLS");
CASE4 (filepath);
System ("pause");
Break
Case 5:exit (0);
Break
Default Break
} return 0;
}
The above is the C + + Traversal folder related operations, I hope to help you learn.