C + + Traverse folder get file list _c language

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.