C++遍歷文件夾獲取文件列表
本文實(shí)例類似遍歷一個(gè)文件夾然后獲得該文件夾下的文件列表,可以隨意切換文件目錄,本來是用在我們小組寫的簡(jiǎn)易ftp服務(wù)器上的一個(gè)給客戶端顯示的一個(gè)小插件,總之單拿出來應(yīng)該沒啥含量,調(diào)用了windows的一些API。
實(shí)例代碼:
#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("請(qǐng)選擇操作\n");
printf("1.顯示當(dāng)前文件夾的所有文件\n");
printf("2.返回上一級(jí)\n");
printf("3.進(jìn)入文件夾\n");
printf("4.進(jìn)入指定文件夾\n");
printf("5.退出\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;
}
以上就是C++遍歷文件夾的相關(guān)操作,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
C++如何采用Daemon進(jìn)行后臺(tái)程序的部署
這篇文章主要介紹了C++采用Daemon進(jìn)行后臺(tái)程序的部署,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
C++利用循環(huán)和棧實(shí)現(xiàn)走迷宮
這篇文章主要為大家詳細(xì)介紹了C++利用循環(huán)和棧實(shí)現(xiàn)走迷宮,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
C++如何將二叉搜索樹轉(zhuǎn)換成雙向循環(huán)鏈表(雙指針或數(shù)組)
這篇文章主要介紹了C++如何將二叉搜索樹轉(zhuǎn)換成雙向循環(huán)鏈表(雙指針或數(shù)組),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
淺析C++字節(jié)對(duì)齊容易被忽略的兩個(gè)問題
今天我就和大家分享一下C++字節(jié)對(duì)齊容易被忽略的兩個(gè)問題。以下問題也是我實(shí)際開發(fā)工作中遇到的,如果有不同意見歡迎交流2013-07-07
c++實(shí)現(xiàn)MD5算法實(shí)現(xiàn)代碼
用c++實(shí)現(xiàn)了md5算法。包含 md5.h 和md5.cpp 兩個(gè)文件。主要參考百度百科 “MD5” 原理,代碼中變量命名也是參考其中的公式,程序的使用說明在md5.h 文件的末尾注釋中2013-11-11

