国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++遍歷文件夾獲取文件列表

2020-01-26 14:36:33
字體:
來源:轉載
供稿:網友

本文實例類似遍歷一個文件夾然后獲得該文件夾下的文件列表,可以隨意切換文件目錄,本來是用在我們小組寫的簡易ftp服務器上的一個給客戶端顯示的一個小插件,總之單拿出來應該沒啥含量,調用了windows的一些API。

實例代碼:

#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("請選擇操作/n");  printf("1.顯示當前文件夾的所有文件/n");  printf("2.返回上一級/n");  printf("3.進入文件夾/n");  printf("4.進入指定文件夾/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++遍歷文件夾的相關操作,希望對大家的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 门头沟区| 修文县| 古蔺县| 清镇市| 建瓯市| 江门市| 富顺县| 五大连池市| 中江县| 衡阳县| 彭州市| 郁南县| 晴隆县| 清新县| 大新县| 仙游县| 安图县| 西乌珠穆沁旗| 新蔡县| 东阳市| 静宁县| 永城市| 迁西县| 龙岩市| 吴川市| 齐河县| 金川县| 黄大仙区| 凤城市| 兰考县| 上蔡县| 河津市| 拉孜县| 石首市| 鸡泽县| 浮梁县| 兴山县| 昌邑市| 白水县| 罗城| 台中县|