實在不知道該怎麼起標題了,類似遍歷一個文件夾然後獲得該文件夾下的文件列表,可以隨意切換文件目錄,是個不是特別大的東西,本來是用在我們小組寫的簡易ftp服務器上的一個給客戶端顯示的一個小插件一樣的東西?總之單拿出來應該沒啥含量,調用了windows的一些API
頭文件包的有些多了,有的沒用,是之前自己的音樂播放器裡面的帶的東西。。。
1 #include<iostream>
2 #include<stdlib.h>
3 #include<windows.h>
4 #include<mmsystem.h>
5 #include<string>
6 #include<time.h>
7 #include<fstream>
8 #include<stdio.h>
9 #include<vector>
10 #include<string>
11 #pragma comment (lib, "winmm.lib")
12 using namespace std;
13
14 void MainMenu()
15 {
16 printf("請選擇操作\n");
17 printf("1.顯示當前文件夾的所有文件\n");
18 printf("2.返回上一級\n");
19 printf("3.進入文件夾\n");
20 printf("4.進入指定文件夾\n");
21 printf("5.退出\n");
22 }
23 void ShowFileList(string filename)
24 {
25 WIN32_FIND_DATAA p;
26 vector<string> filelist;
27 HANDLE h = FindFirstFileA(filename.c_str(), &p);
28 filelist.push_back(p.cFileName);
29 while (FindNextFileA(h, &p))
30 {
31 filelist.push_back(p.cFileName);
32 if (filelist.back() == "." || filelist.back() == "..")
33 {
34 filelist.pop_back();
35 }
36 }
37 for (int i = 0; i < filelist.size(); i++)
38 {
39 cout << filelist[i] << endl;
40 }
41 }
42
43 void ShowLastFileList(string & filepath)
44 {
45 string filepath2 = filepath;
46 string tmp = "../";
47 tmp += filepath2;
48 filepath = tmp;
49 ShowFileList(tmp);
50 }
51 void ShowSelectFileList(string filepath)
52 {
53 string yourchoose;
54 cin >> yourchoose;
55 yourchoose += '/';
56 string filepath2 = filepath;
57 yourchoose += filepath2;
58 ShowFileList(yourchoose);
59 }
60 void case4(string filepath)
61 {
62 string filename;
63 cin >> filename;
64 filename += '/';
65 filename += filepath;
66 ShowFileList(filename);
67 }
68 int main()
69 {
70 string filepath;
71 filepath = "*.*";
72 string filePath = filepath;
73 while (1)
74 {
75 system("CLS");
76 MainMenu();
77 int n;
78 cin >> n;
79 switch (n)
80 {
81 case 1:
82 system("CLS");
83 ShowFileList(filePath);
84 system("pause");
85 break;
86 case 2:
87 system("CLS");
88 ShowLastFileList(filePath);
89 system("pause");
90 break;
91 case 3:
92 system("CLS");
93 ShowSelectFileList(filePath);
94 system("pause");
95 break;
96 case 4:
97 system("CLS");
98 case4(filepath);
99 system("pause");
100 break;
101 case 5:
102 exit(0);
103 break;
104 default:
105 break;
106 }
107 }
108 return 0;
109 }